Equivalent Sets 【至少加几边使图为强联通】+【 scc +缩点】

来源:互联网 发布:战天堂翅膀进阶数据 编辑:程序博客网 时间:2024/05/17 01:47

To prove two sets A and B are equivalent, we can first prove A is a subset of B, and then prove B is a subset of A, so finally we got that these two sets are equivalent.
You are to prove N sets are equivalent, using the method above: in each step you can prove a set X is a subset of another set Y, and there are also some sets that are already proven to be subsets of some other sets.
Now you want to know the minimum steps needed to get the problem proved.
Input
The input file contains multiple test cases, in each case, the first line contains two integers N <= 20000 and M <= 50000.
Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.
Output
For each case, output a single integer: the minimum steps needed.
Sample Input
4 0
3 2
1 2
1 3
Sample Output
4
2

Hint
Case 2: First prove set 2 is a subset of set 1 and then prove set 3 is a subset of set 1.

思路: 扫一遍SCC+缩点 ,图为有向无环图 ,计数 每个新点的入度和出度,对于一个强联通的图来说,每个点入度和出度都肯定有。。
所以统计出 入度为0的点个数,出度为0的点个数; 然后就开始 互相连线,如果入度为0为2个,出度为0的为1个,则用那个出度为0的点和其中一个入度为0的相连就可以了。另外其它的点连那个入度为0的点就可以了。所以 我们只要找到 其中最大值 就可以了
代码

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<map>#include<vector>#include<set>#define CLR(a,b) memset((a),(b),sizeof(a))#define inf 0x3f3f3f3f#define mod 100009#define LL long long#define M   50000+100#define ll o<<1#define rr o<<1|1#define lson o<<1,l,mid#define rson o<<1|1,mid+1,rusing namespace std;void read(int &x){    x=0;char c;    while((c=getchar())<'0');    do x=x*10+c-'0';while((c=getchar())>='0');}struct Edge {    int from,to,next;}edge[M];int head[M],top;int n,m;vector<int>G[M];vector<int >scc[M];int scc_cnt,dfs_clock;int low[M],dfn[M],in[M],out[M];stack<int>S;int Instack[M];int sccno[M];void init(){    top=0;    memset(head,-1,sizeof(head));    while(!S.empty()) S.pop();}void addedge(int a,int b){    Edge e={a,b,head[a]};    edge[top]=e; head[a]=top++;}void getmap(){    int i,j;int a,b;    for(i=0;i<m;i++)    {        read(a);read(b);        addedge(a,b);    }}void tarjan(int now){    int nexts;    low[now]=dfn[now]=++dfs_clock;    S.push(now);Instack[now]=1;    for(int i=head[now];i!=-1;i=edge[i].next)    {        Edge e=edge[i];        if(!dfn[e.to])        {          tarjan(e.to);          low[now]=min(low[e.to],low[now]);        }        else if(Instack[e.to])        low[now]=min(low[now],dfn[e.to]);    }    if(dfn[now]==low[now])     {        scc_cnt++;        scc[scc_cnt].clear();        for(;;)        {        nexts=S.top();S.pop();Instack[nexts]=0;        sccno[nexts]=scc_cnt;        scc[scc_cnt].push_back(nexts);        if(nexts==now) break;        }    }}void find_cut(int le,int ri){    memset(low,0,sizeof(low));    memset(dfn,0,sizeof(dfn));    memset(Instack,0,sizeof(Instack));    memset(sccno,0,sizeof(sccno));    scc_cnt=dfs_clock=0;    for(int i=le;i<=ri;i++)    if(!dfn[i]) tarjan(i);}void suodian(){    int i,j;    for(i=1;i<=scc_cnt;i++) G[i].clear(),in[i]=0,out[i]=0;    for(i=0;i<top;i++)    {        int u=sccno[edge[i].from];        int v=sccno[edge[i].to];        if(v!=u)         {            G[u].push_back(v);            in[v]++;out[u]++;  // 统计新点的入度和出度        }    }}void solve(){    int i,j;int a,b;a=b=0;    if(scc_cnt==1) printf("0\n"); // 特殊情况 要讨论下    else     {        for(i=1;i<=scc_cnt;i++)        {        if(!in[i]) a++;        if(!out[i]) b++;        }           printf("%d\n",max(a,b));    }}int main(){    while(~scanf("%d%d",&n,&m))    {        init();        getmap();        find_cut(1,n);        suodian();        solve();    }    return 0;}
0 0
原创粉丝点击