标签:rar input sep 奇数 recent miss cst ios cannot
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 12439 | Accepted: 4126 |
Description
Input
Output
Sample Input
5 5 1 4 1 5 2 5 3 4 4 5 0 0
Sample Output
2
Hint
Source
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define C(x) memset(x,0,sizeof(x)) using namespace std; const int N=1005,M=1e6+5; inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x*f; } int n=0,m,u,v,g[N][N]; struct edge{ int v,ne; }e[M<<1]; int h[N],cnt=0; inline void ins(int u,int v){ cnt++; e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt; cnt++; e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt; } void buildGraph(){ memset(h,0,sizeof(h)); cnt=0; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) if(!g[i][j]) ins(i,j); } struct data{ int u,v; data(int a=0,int b=0):u(a),v(b){} }st[M<<1]; int top=0; int dfn[N],low[N],iscut[N],belong[N],dfc=0,bcc=0; int col[N],ok[N]; bool color(int u,int id){//printf("%d %d %d\n",u,col[u],id); for(int i=h[u];i;i=e[i].ne){ int v=e[i].v; if(belong[v]!=id) continue; if(col[v]==col[u]) return 0;//first do this if(!col[v]){ col[v]=3-col[u]; if(!color(v,id)) return 0; } } return 1; } void dfs(int u,int fa){ dfn[u]=low[u]=++dfc; int child=0; for(int i=h[u];i;i=e[i].ne){ int v=e[i].v; if(!dfn[v]){ st[++top]=data(u,v); child++; dfs(v,u); low[u]=min(low[u],low[v]); if(low[v]>=dfn[u]){ iscut[u]=1; bcc++; while(true){ int tu=st[top].u,tv=st[top--].v; if(belong[tu]!=bcc) belong[tu]=bcc; if(belong[tv]!=bcc) belong[tv]=bcc; if(tu==u&&tv==v) break; } col[u]=1; if(!color(u,bcc)) for(int i=1;i<=n;i++) if(belong[i]==bcc) ok[i]=1; col[u]=0;//for cut vertex } }else if(dfn[v]<dfn[u]&&v!=fa){ st[++top]=data(u,v);//notice!!! low[u]=min(low[u],dfn[v]); } } if(child==1&&fa==0) iscut[u]=0; } void BCC(){ dfc=bcc=0;top=0; C(dfn);C(low);C(iscut);C(belong); C(col);C(ok); for(int i=1;i<=n;i++) if(!dfn[i]) dfs(i,0); } int main(){ while(scanf("%d%d",&n,&m)!=EOF&&(n||m)){ memset(g,0,sizeof(g)); for(int i=1;i<=m;i++){u=read();v=read();g[u][v]=g[v][u]=1;} buildGraph(); BCC(); int ans=0; for(int i=1;i<=n;i++) if(!ok[i]) ans++; printf("%d\n",ans); } }
POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
标签:rar input sep 奇数 recent miss cst ios cannot
原文地址:http://www.cnblogs.com/candy99/p/6021467.html