Equivalent setsTime limit:12000/4000 MS (java/others) Memory limit:104857/104857 K (java/others)
Total submission (s): 3065 Accepted Submission (s): 1077
Problem Descriptionto prove-sets A and B are equivalent, we can first prove a was a subset of B, and then prove B is a Subset of A, so-finally we got that these and sets are equivalent.
You be to prove N sets be equivalent, using the method above:in each step can prove a set X is a subset of another Set Y, and there is also some sets that is already proven to being subsets of some other sets.
Now, want to know the minimum steps needed to get the problem proved.
Inputthe input file contains multiple test cases, in each case, the first line contains both integers N <= 20000 and M & lt;= 50000.
Next M lines, each line contains the integers x, Y, means set X in a subset of set Y.
Outputfor, output a single integer:the minimum steps needed.
Sample Input
4 03 21) 21 3
Sample Output
42 Analysis: Test instructions: There are 1~n to represent n conclusions, first has given m relations, a B for a can prove B, for at least a few side can be added to be N conclusion 22 can prove, it is obvious that at least add a few sides to make a strong unicom. The first indentation, in the degree of access is 0, and the number of degrees of 0, take its maximum this is the answer.
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- #include <iostream>
- #include <stack>
- #define MAX (A, b) a>b?a:b
- #define MAXH 220000+10
- #define MAXN 550000+10
- using namespace Std;
- typedef struct{
- int to,next;
- }node;
- stack<int>s;
- Node E1[MAXN],E2[MAXN];
- int head1[maxh],head2[maxh],mark[maxh],cnt1,cnt2;
- int belong[maxh],num[maxh][2],in[maxh],out[maxh],count;
- void init (int n) {
- memset (head1,-1,sizeof (HEAD1));
- memset (head2,-1,sizeof (head2));
- cnt1=0;
- cnt2=0;
- }
- void Add (int a,int b) {
- e1[cnt1].to=b,e1[cnt1].next=head1[a],head1[a]=cnt1++;
- e2[cnt2].to=a,e2[cnt2].next=head2[b],head2[b]=cnt2++;
- }
- void dfs1 (int x) {
- Mark[x]=1;
- for (int i=head1[x];i+1;i=e1[i].next) {
- int to=e1[i].to;
- if (Mark[to])
- Continue
- DFS1 (to);
- }
- S.push (x);
- }
- void dfs2 (int x) {
- Mark[x]=1;
- Belong[x]=count;
- for (int i=head2[x];i+1;i=e2[i].next) {
- int to=e2[i].to;
- if (Mark[to])
- Continue
- DFS2 (to);
- }
- }
- int main () {
- int n,m,a,b,sumin,sumout;
- while (~SCANF ("%d%d", &n,&m)) {
- Init (n);
- for (int i=0;i<m;i++) {
- scanf ("%d%d", &a,&b);
- Num[i][0]=a;
- Num[i][1]=b;
- Add (A, b);
- }
- memset (Mark,0,sizeof (Mark));
- while (! S.empty ())
- S.pop ();
- for (int i=1;i<=n;i++) {
- if (!mark[i]) {
- DFS1 (i);
- }
- }
- memset (Mark,0,sizeof (Mark));
- count=0;
- while (! S.empty ()) {
- int S=s.top ();
- S.pop ();
- if (!mark[s]) {
- count++;
- DFS2 (s);
- }
- }
- if (count==1) {
- printf ("0\n");
- Continue
- }
- memset (In,0,sizeof (in));
- Memset (out,0,sizeof (out));
- sumin=sumout=0;
- for (int i=0;i<m;i++) {
- if (Belong[num[i][0]]!=belong[num[i][1]])
- In[belong[num[i][0]]]=out[belong[num[i][1]]]=1;
- }
- for (int i=1;i<=count;i++) {
- if (!in[i]) {
- sumin++;
- }
- if (!out[i]) {
- sumout++;
- }
- }
- printf ("%d\n", Max (Sumin,sumout));
- }
- return 0;
- }
HDU 3836 equivalent Sets "Strong Unicom"