程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 周賽 HDU 2767 1269 1872 強連通

周賽 HDU 2767 1269 1872 強連通

編輯:C++入門知識

HDU 2767

題意:給出一些點之間的關系,然後問最少添加多少條邊可以使這張圖強連通。

裸題,唯一的trick就是判斷圖一開始就是強連通圖的時候輸出為0,這裡沒想清楚,導致卡了半小時。


[cpp]
#include <iostream>  
#include <cstdio>  
#include <algorithm>  
#include <string>  
#include <cmath>  
#include <cstring>  
#include <queue>  
#include <set>  
#include <vector>  
#include <stack>  
#include <map>  
#include <iomanip>  
#define PI acos(-1.0)  
#define Max 100005  
#define inf 1<<28  
#define LL(x) (x<<1)  
#define RR(x) (x<<1|1)  
#define FOR(i,s,t) for(int i=(s);i<=(t);++i)  
#define ll long long  
#define mem(a,b) memset(a,b,sizeof(a))  
#define mp(a,b) make_pair(a,b)  
 
using namespace std; 
 
struct kdq 

    int e ,next ; 
} ed[Max * 10] ; 
int head[Max] ,num1 ; 
int dfn[Max] ,low[Max] , vis[Max] , st[Max] , in[Max] ,out[Max] ,belong[Max] ,cnt[Max] ; 
int tp = 0 ,dp = 0 ,num = 0 ; 
 
void add(int a ,int b) 

    ed[num1].e = b ; 
    ed[num1].next = head[a] ; 
    head[a] = num1 ++ ; 

void init() 

    mem(dfn,-1) ; 
    mem(low,0) ; 
    mem(vis,0) ; 
    mem(st,0) ; 
    mem(in,0) ; 
    mem(out,0) ; 
    mem(belong,0) ; 
    mem(head,-1) ; 
    mem(cnt,0) ; 
    num = num1 = tp = dp = 0 ; 

void tarjan(int now) 

    vis[now] = 1 ; 
    st[tp ++ ] = now ; 
    dfn[now] = low[now] = dp ++ ; 
    for (int i = head[now] ; i != -1 ; i = ed[i].next ) 
    { 
        int v = ed[i].e ; 
        if(dfn[v] == -1) 
        { 
            tarjan(v) ; 
            low[now] = min(low[now] ,low[v]) ; 
        } 
        else if(vis[v]) 
        { 
            low[now] = min(low[now] ,dfn[v]) ; 
        } 
    } 
    if(low[now] == dfn[now]) 
    { 
        int xx ; 
        num ++ ; 
        do 
        { 
            xx = st[-- tp ] ; 
            vis[xx] = 0 ; 
            belong[xx] = num ; 
            cnt[num] ++ ; 
        } 
        while(xx != now) ; 
    } 

void solve() 

    int n ,m ; 
    int T ; 
    while(cin >> T ) 
    { 
        while( T -- ) 
        { 
            cin >> n>> m ; 
            init() ; 
            for (int i = 0 ; i < m ; i ++) 
            { 
                int a , b ; 
                scanf("%d%d",&a,&b) ; 
                add(a,b) ; 
            } 
 
            for (int i = 1 ; i <= n ; i ++ ) 
                if(dfn[i] == -1)tarjan(i) ; 
            for (int i = 1 ; i <= n ; i ++ ) 
                for (int j = head[i] ; j != -1 ; j = ed[j].next ) 
                { 
                    int x = belong[i] ; 
                    int y = belong[ed[j].e] ; 
                    if(x != y ) 
                    { 
                        out[x] ++ ; 
                        in[y] ++ ; 
                    } 
                } 
            int ans = 0 ; 
            int ans1 = 0 ; 
            int kk = 0 ; 
            mem(vis,0) ; 
 
            for (int i  = 1 ; i <= num ; i ++) 
                { 
                    if(in[i] == 0)ans ++ ; 
                    if(out[i] == 0)ans1 ++ ; 
                } 
            ans1 = max(ans,ans1) ; 
            //ans1 += ans ;  
            if(num == 1)ans1 = 0 ; 
            cout <<ans1 <<endl; 
        } 
 
    } 

int main() 

    solve() ; 
    return 0; 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <iomanip>
#define PI acos(-1.0)
#define Max 100005
#define inf 1<<28
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define FOR(i,s,t) for(int i=(s);i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)

using namespace std;

struct kdq
{
    int e ,next ;
} ed[Max * 10] ;
int head[Max] ,num1 ;
int dfn[Max] ,low[Max] , vis[Max] , st[Max] , in[Max] ,out[Max] ,belong[Max] ,cnt[Max] ;
int tp = 0 ,dp = 0 ,num = 0 ;

void add(int a ,int b)
{
    ed[num1].e = b ;
    ed[num1].next = head[a] ;
    head[a] = num1 ++ ;
}
void init()
{
    mem(dfn,-1) ;
    mem(low,0) ;
    mem(vis,0) ;
    mem(st,0) ;
    mem(in,0) ;
    mem(out,0) ;
    mem(belong,0) ;
    mem(head,-1) ;
    mem(cnt,0) ;
    num = num1 = tp = dp = 0 ;
}
void tarjan(int now)
{
    vis[now] = 1 ;
    st[tp ++ ] = now ;
    dfn[now] = low[now] = dp ++ ;
    for (int i = head[now] ; i != -1 ; i = ed[i].next )
    {
        int v = ed[i].e ;
        if(dfn[v] == -1)
        {
            tarjan(v) ;
            low[now] = min(low[now] ,low[v]) ;
        }
        else if(vis[v])
        {
            low[now] = min(low[now] ,dfn[v]) ;
        }
    }
    if(low[now] == dfn[now])
    {
        int xx ;
        num ++ ;
        do
        {
            xx = st[-- tp ] ;
            vis[xx] = 0 ;
            belong[xx] = num ;
            cnt[num] ++ ;
        }
        while(xx != now) ;
    }
}
void solve()
{
    int n ,m ;
    int T ;
    while(cin >> T )
    {
        while( T -- )
        {
            cin >> n>> m ;
            init() ;
            for (int i = 0 ; i < m ; i ++)
            {
                int a , b ;
                scanf("%d%d",&a,&b) ;
                add(a,b) ;
            }

            for (int i = 1 ; i <= n ; i ++ )
                if(dfn[i] == -1)tarjan(i) ;
            for (int i = 1 ; i <= n ; i ++ )
                for (int j = head[i] ; j != -1 ; j = ed[j].next )
                {
                    int x = belong[i] ;
                    int y = belong[ed[j].e] ;
                    if(x != y )
                    {
                        out[x] ++ ;
                        in[y] ++ ;
                    }
                }
            int ans = 0 ;
            int ans1 = 0 ;
            int kk = 0 ;
            mem(vis,0) ;

            for (int i  = 1 ; i <= num ; i ++)
                {
                    if(in[i] == 0)ans ++ ;
                    if(out[i] == 0)ans1 ++ ;
                }
            ans1 = max(ans,ans1) ;
            //ans1 += ans ;
            if(num == 1)ans1 = 0 ;
            cout <<ans1 <<endl;
        }

    }
}
int main()
{
    solve() ;
    return 0;
}
HDU 1269

裸題


[cpp]
#include <iostream>  
#include <cstdio>  
#include <algorithm>  
#include <string>  
#include <cmath>  
#include <cstring>  
#include <queue>  
#include <set>  
#include <vector>  
#include <stack>  
#include <map>  
#include <iomanip>  
#define PI acos(-1.0)  
#define Max 100005  
#define inf 1<<28  
#define LL(x) (x<<1)  
#define RR(x) (x<<1|1)  
#define FOR(i,s,t) for(int i=(s);i<=(t);++i)  
#define ll long long  
#define mem(a,b) memset(a,b,sizeof(a))  
#define mp(a,b) make_pair(a,b)  
#define PII pair<int,int>  
using namespace std; 
struct kdq 

    int e, l, next ; 
}ed[Max] ; 
 
int head[Max] ; 
int num1 ; 
void add(int a,int b) 

    ed[num1].e = b ; 
    ed[num1].next = head[a] ; 
    head[a] = num1 ++ ; 

int dfn[Max] ,low[Max] ,st[Max] ,vis[Max] ; 
int belong[Max] ; 
int dp , tp ,num ; 
void init() 

    mem(head,-1) ; 
    num1 = 0 ; 
    mem(dfn,-1) ; 
    mem(low,0) ; 
    mem(st,0) ; 
    mem(vis,0) ; 
    mem(belong ,0) ; 
    dp = 0 ; 
    tp = 0 ; 
    num = 0 ; 

void tarjan(int now) 

    dfn[now] = low[now] = dp ++ ; 
    vis[now] = 1 ; 
    st[tp ++ ] = now ; 
    for (int i = head[now] ; i != -1 ;i = ed[i].next ) 
    { 
        int e = ed[i].e ; 
        if(dfn[e] == -1) 
        {tarjan(e); 
        low[now] = min(low[now],low[e]) ; 
        } 
        else if(vis[e]) 
        low[now] = min(low[now],dfn[e]) ; 
    } 
    if(dfn[now] == low[now]) 
    { 
        num ++ ; 
        int xx ; 
        do 
        { 
            xx = st[-- tp] ; 
            vis[xx] = 0 ; 
            belong[xx] = num ; 
        } 
        while(xx != now) ; 
    } 

int main() 

    int n ,m ; 
    while(scanf("%d%d",&n,&m),(n + m)) 
    { 
        init() ; 
        for (int i = 0 ;i < m ;i ++) 
        { 
            int a ,b ; 
            scanf("%d%d",&a,&b) ; 
            add(a,b) ; 
        } 
        for (int i = 1 ;i <= n; i ++)if(dfn[i] == -1)tarjan(i) ; 
        //cout <<num<<endl;  
        if(num == 1)cout <<"Yes"<<endl; 
        else 
        cout <<"No"<<endl; 
    } 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <iomanip>
#define PI acos(-1.0)
#define Max 100005
#define inf 1<<28
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define FOR(i,s,t) for(int i=(s);i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define PII pair<int,int>
using namespace std;
struct kdq
{
    int e, l, next ;
}ed[Max] ;

int head[Max] ;
int num1 ;
void add(int a,int b)
{
    ed[num1].e = b ;
    ed[num1].next = head[a] ;
    head[a] = num1 ++ ;
}
int dfn[Max] ,low[Max] ,st[Max] ,vis[Max] ;
int belong[Max] ;
int dp , tp ,num ;
void init()
{
    mem(head,-1) ;
    num1 = 0 ;
    mem(dfn,-1) ;
    mem(low,0) ;
    mem(st,0) ;
    mem(vis,0) ;
    mem(belong ,0) ;
    dp = 0 ;
    tp = 0 ;
    num = 0 ;
}
void tarjan(int now)
{
    dfn[now] = low[now] = dp ++ ;
    vis[now] = 1 ;
    st[tp ++ ] = now ;
    for (int i = head[now] ; i != -1 ;i = ed[i].next )
    {
        int e = ed[i].e ;
        if(dfn[e] == -1)
        {tarjan(e);
        low[now] = min(low[now],low[e]) ;
        }
        else if(vis[e])
        low[now] = min(low[now],dfn[e]) ;
    }
    if(dfn[now] == low[now])
    {
        num ++ ;
        int xx ;
        do
        {
            xx = st[-- tp] ;
            vis[xx] = 0 ;
            belong[xx] = num ;
        }
        while(xx != now) ;
    }
}
int main()
{
    int n ,m ;
    while(scanf("%d%d",&n,&m),(n + m))
    {
        init() ;
        for (int i = 0 ;i < m ;i ++)
        {
            int a ,b ;
            scanf("%d%d",&a,&b) ;
            add(a,b) ;
        }
        for (int i = 1 ;i <= n; i ++)if(dfn[i] == -1)tarjan(i) ;
        //cout <<num<<endl;
        if(num == 1)cout <<"Yes"<<endl;
        else
        cout <<"No"<<endl;
    }
}

HDU 1872

題意:中文題,不解釋。

思路:tarjan,縮點,然後找出入度為0的聯通分量,然後求出這些分量裡花費最少的一個人,相加即可。


[cpp] 
#include <iostream>  
#include <cstdio>  
#include <algorithm>  
#include <string>  
#include <cmath>  
#include <cstring>  
#include <queue>  
#include <set>  
#include <vector>  
#include <stack>  
#include <map>  
#include <iomanip>  
#define PI acos(-1.0)  
#define Max 100005  
#define inf 1<<28  
#define LL(x) (x<<1)  
#define RR(x) (x<<1|1)  
#define FOR(i,s,t) for(int i=(s);i<=(t);++i)  
#define ll long long  
#define mem(a,b) memset(a,b,sizeof(a))  
#define mp(a,b) make_pair(a,b)  
#define PII pair<int,int>  
using namespace std; 
struct kdq 

    int e, l, next ; 
} ed[Max] ; 
int cost[Max] ; 
int head[Max] ; 
int num1 ; 
void add(int a,int b) 

    ed[num1].e = b ; 
    ed[num1].next = head[a] ; 
    head[a] = num1 ++ ; 

int dfn[Max] ,low[Max] ,st[Max] ,vis[Max] ; 
int belong[Max] ; 
int dp , tp ,num ; 
int in[Max] ,out[Max] ;vector<int>qq[Max] ; 
void init(int n) 

    mem(head,-1) ; 
    num1 = 0 ; 
    mem(dfn,-1) ; 
    mem(low,0) ; 
    mem(st,0) ; 
    mem(vis,0) ; 
    mem(belong ,0) ; 
    dp = 0 ; 
    tp = 0 ; 
    num = 0 ; 
    mem(in,0) ; 
    mem(out,0) ; 
    for (int i = 0 ;i <= n ;i ++)qq[i].clear() ; 

 
void tarjan(int now) 

    dfn[now] = low[now] = dp ++ ; 
    vis[now] = 1 ; 
    st[tp ++ ] = now ; 
    for (int i = head[now] ; i != -1 ; i = ed[i].next ) 
    { 
        int e = ed[i].e ; 
        if(dfn[e] == -1) 
        { 
            tarjan(e); 
            low[now] = min(low[now],low[e]) ; 
        } 
        else if(vis[e]) 
            low[now] = min(low[now],dfn[e]) ; 
    } 
    if(dfn[now] == low[now]) 
    { 
        num ++ ; 
        int xx ; 
        do 
        { 
            xx = st[-- tp] ; 
            vis[xx] = 0 ; 
            qq[num].push_back(xx) ; 
            belong[xx] =num ; 
        } 
        while(xx != now) ; 
    } 

int main() 

    int n ,m ; 
    while(scanf("%d%d",&n,&m) != EOF) 
    { 
        init(n) ; 
        for (int i = 1 ; i <= n ; i ++)scanf("%d",&cost[i]) ; 
        for (int i = 0 ; i < m ; i ++) 
        { 
            int a ,b ; 
            scanf("%d%d",&a,&b) ; 
            add(a,b) ; 
        } 
        for (int i = 1 ; i <= n; i ++)if(dfn[i] == -1)tarjan(i) ; 
        for (int i = 1 ; i  <= n ; i ++) 
        { 
            for (int j = head[i] ; j != -1 ; j = ed[j].next ) 
            { 
                int x = belong[i] ; 
                int y = belong[ed[j].e] ; 
                if(x != y ) 
                { 
                    out[x] ++ ; 
                    in[y] ++ ; 
                } 
            } 
        } 
 
        ll aa = 0 ; 
        int cc = 0 ; 
        int sum = inf ; 
        int i ; 
        for (i = 1 ; i <= num ; i ++) 
        { 
            if(in[i] == 0) 
            { 
                cc ++ ; 
                sum = inf ; 
                int l = qq[i].size() ; 
                for (int k = 0 ; k  < l ; k ++) 
                    sum = min(sum,cost[qq[i][k]]) ; 
                aa += sum ; 
            } 
        } 
        cout <<cc <<" "<<aa <<endl; 
    } 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <iomanip>
#define PI acos(-1.0)
#define Max 100005
#define inf 1<<28
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define FOR(i,s,t) for(int i=(s);i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define PII pair<int,int>
using namespace std;
struct kdq
{
    int e, l, next ;
} ed[Max] ;
int cost[Max] ;
int head[Max] ;
int num1 ;
void add(int a,int b)
{
    ed[num1].e = b ;
    ed[num1].next = head[a] ;
    head[a] = num1 ++ ;
}
int dfn[Max] ,low[Max] ,st[Max] ,vis[Max] ;
int belong[Max] ;
int dp , tp ,num ;
int in[Max] ,out[Max] ;vector<int>qq[Max] ;
void init(int n)
{
    mem(head,-1) ;
    num1 = 0 ;
    mem(dfn,-1) ;
    mem(low,0) ;
    mem(st,0) ;
    mem(vis,0) ;
    mem(belong ,0) ;
    dp = 0 ;
    tp = 0 ;
    num = 0 ;
    mem(in,0) ;
    mem(out,0) ;
    for (int i = 0 ;i <= n ;i ++)qq[i].clear() ;
}

void tarjan(int now)
{
    dfn[now] = low[now] = dp ++ ;
    vis[now] = 1 ;
    st[tp ++ ] = now ;
    for (int i = head[now] ; i != -1 ; i = ed[i].next )
    {
        int e = ed[i].e ;
        if(dfn[e] == -1)
        {
            tarjan(e);
            low[now] = min(low[now],low[e]) ;
        }
        else if(vis[e])
            low[now] = min(low[now],dfn[e]) ;
    }
    if(dfn[now] == low[now])
    {
        num ++ ;
        int xx ;
        do
        {
            xx = st[-- tp] ;
            vis[xx] = 0 ;
            qq[num].push_back(xx) ;
            belong[xx] =num ;
        }
        while(xx != now) ;
    }
}
int main()
{
    int n ,m ;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        init(n) ;
        for (int i = 1 ; i <= n ; i ++)scanf("%d",&cost[i]) ;
        for (int i = 0 ; i < m ; i ++)
        {
            int a ,b ;
            scanf("%d%d",&a,&b) ;
            add(a,b) ;
        }
        for (int i = 1 ; i <= n; i ++)if(dfn[i] == -1)tarjan(i) ;
        for (int i = 1 ; i  <= n ; i ++)
        {
            for (int j = head[i] ; j != -1 ; j = ed[j].next )
            {
                int x = belong[i] ;
                int y = belong[ed[j].e] ;
                if(x != y )
                {
                    out[x] ++ ;
                    in[y] ++ ;
                }
            }
        }

        ll aa = 0 ;
        int cc = 0 ;
        int sum = inf ;
        int i ;
        for (i = 1 ; i <= num ; i ++)
        {
            if(in[i] == 0)
            {
                cc ++ ;
                sum = inf ;
                int l = qq[i].size() ;
                for (int k = 0 ; k  < l ; k ++)
                    sum = min(sum,cost[qq[i][k]]) ;
                aa += sum ;
            }
        }
        cout <<cc <<" "<<aa <<endl;
    }
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved