Description
The territory of Byteotia was occupied, and King Byteasar was planning to organize a secret resistance movement. The king needed to elect some people to carry out the campaign, and these people were divided into two parts: part of the Conspiracy to act in the occupied area, and the other part to operate the logistics organization in the occupied territories. But there's a problem here: 1. Any two people in the logistics organization must be acquaintances in order to promote cooperation and improve efficiency. 2. No two persons in an accomplice's group can be acquaintances. 3. There must be at least one person in each of the sections. The king would like to know how many distribution schemes meet the above conditions and, of course, there may not be a reasonable plan. Now the king will put this problem to your disposal!
Input
The first line of an integer n (2<=n<=5000) indicates that there are n individuals involved in the resistance movement, labeled 1. N. Then there are n lines, the first number of lines I ki (0<=ki<=n-1) indicates I know ki individual, then the number of Ki represents I acquaintance. P.S. Input satisfies: If I is an acquaintance of X, X will appear in the sequence of I and I will also appear in the acquaintance sequence of X.
Output
The total number of scenarios that meet the criteria.
Sample Input
4
2 2 3
2 1 3
3 1 2 4
1 3
Sample Output3HINT
Hint 1 and 4 points to the collaborators ' organizations, 2 and 3 as logistics organizations. 2 and 4 points to the collaborators ' organizations, 1 and 3 are logistics organizations. 4 are separately divided into accomplices organizations, 1 and 2, and 3 are logistics organizations.
Exercises
Consider constructing a set of feasible solutions, splitting each point into two points x0,x1,x0 represents a logistic organization, X1 represents an accomplice.
If x and Y are known, then X1 to y0.
If x and Y do not know, then x0 to the Y1 edge.
So we can find a feasible solution for a group of 2-sat, if there is no solution, the answer is 0.
If there is a solution, then a maximum of one person from the logistics organization to the accomplice, but also at most can be a person from the accomplice to the logistics organization, but also one in the accomplice of another in the logistics organization of the two persons exchange.
Apparently not directly violent.
First preprocess the number of points and any conflicting points in each point and it
The conflict point is defined as the point at which the point in the opposite direction will conflict with the dot if it is placed in the opposite direction.
For example, if two people u and V,u in the logistics, V in the accomplice, while the u,v know each other, then V is the contradiction point of U
or two persons A and b,a in the accomplice, B in the logistics, and a A, B is not known, this is a contradiction point of a
Let's talk about the situation before you exchange.
Obviously, when the number of contradictions in a point exceeds 2, he can neither go directly to the opposite side nor exchange directly with someone on the opposite side.
When a point u's contradiction point is 1 o'clock, set its contradiction point to V
If V has only one contradiction point, and V's contradiction point is U, obviously these two people can exchange (pay attention not to repeat calculation)
If V has no contradiction, it is clear that it can be exchanged directly.
In the logistics organization, there is no contradiction in the number of points is T0, the number of accomplices without contradiction points of T1, it is obvious that these two types of points can be any pair to exchange, the program number is T0*T1
Discuss the situation directly to the opposite side.
If there is no contradiction in the point and he is in the organization of more than 1 people (to ensure that at least one person on each side), then he can go to the opposite
And then the initial solution if there is at least one person on each side, then this is a legitimate solution.
Code
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6 #defineMAXN 50057 using namespacestd;8 Charch;9 BOOLOK;Ten voidReadint&x) { One for(ok=0, Ch=getchar ();! IsDigit (CH); Ch=getchar ())if(ch=='-') ok=1; A for(x=0; isdigit (ch); x=x*Ten+ch-'0', ch=GetChar ()); - if(OK) x=-x; - } the intN,K,X,CON[MAXN],CNT0,CNT1,LIST0[MAXN],LIST1[MAXN],NUM[MAXN],BOOM[MAXN]; - BOOLG[MAXN][MAXN],BO[MAXN]; - structgraph{ - inttot,now[maxn<<1],son[maxn*maxn],pre[maxn*MAXN]; + intidx,dfn[maxn<<1],low[maxn<<1],top,stack[maxn],cnt,bel[maxn<<1]; - BOOL inch[maxn<<1]; + voidPutintAintb) {pre[++tot]=now[a],now[a]=tot,son[tot]=b;} A voidDfsintu) { atDfn[u]=low[u]=++idx,stack[++top]=u,inch[u]=1; - for(intP=now[u],v=son[p];p; p=pre[p],v=Son[p]) - if(!dfn[v]) Dfs (v), low[u]=min (low[u],low[v]); - Else if(inch[v]) low[u]=min (low[u],dfn[v]); - if(dfn[u]==Low[u]) { - intV ++CNT; in Do{v=stack[top--],inch[v]=0, bel[v]=cnt;} while(v!=u); - } to } + }g; - intMain () { the read (n); * for(intI=1; i<=n;i++){ $Read (k), con[i]=K;Panax Notoginseng while(k--) Read (x), g[i][x]=1; - } the for(intI=1; i<=n;i++) for(intj=1; j<=n;j++)if(i!=j) { + if(G[i][j]) G.put ((i<<1)+1,j<<1);ElseG.put (i<<1, (j<<1)+1); A } the for(intI=2; I<= (n<<1)+1; i++)if(!G.dfn[i]) G.dfs (i); + for(intI=1; i<=n;i++){ - if(g.bel[i<<1]==g.bel[(i<<1)+1]) {puts ("0");return 0;} $ if(g.bel[i<<1]<g.bel[(i<<1)+1]) list0[++cnt0]=i; $ Elsebo[i]=1, list1[++cnt1]=i; - } - intAns= (cnt0&&cnt1); the for(intI=1; i<=cnt0;i++) for(intj=1; j<=cnt1;j++) - if(G[list0[i]][list1[j]]) num[list0[i]]++,boom[list0[i]]=List1[j];Wuyi for(intI=1; i<=cnt1;i++) for(intj=1; j<=cnt0;j++) the if(!g[list1[i]][list0[j]]) num[list1[i]]++,boom[list1[i]]=List0[j]; - for(intI=1; i<=n;i++)if(num[i]==1){ Wu if(num[boom[i]]==1&&boom[i]>i&&boom[boom[i]]==i) ans++; - Else if(!num[boom[i]]) ans++; About } $ intt0=0, t1=0; - for(intI=1; i<=n;i++)if(!Num[i]) { - if((bo[i]&&cnt1>1)|| (!bo[i]&&cnt0>1)) ans++; - if(Bo[i]) t1++;Elset0++; A } +Ans+=t1*t0; theprintf"%d\n", ans); - return 0; $ } the
bzoj2215: [poi2011]conspiracy