Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
奇怪,我改成STL的就AC了,不明白邻接表怎么就出问题了In Reply To:简单的一个LCA,可是红了一上午,别的问题还好,偏偏是个RE,路过的同志们麻烦顺便帮我看看,感激不尽 Posted by:aliu0932 at 2009-12-05 10:50:01 #include<stdio.h> #include<memory.h> #include<vector> using namespace std; #define N 1001 #define Clear(a) memset(a,0,sizeof(a)) vector<int> E[N],Q[N]; int n,m,root; int father[N],count[N],color[N],anc[N],d[N]; void InitData() { int u,v,i,j,k; Clear(d); Clear(count); Clear(color); for(i=1;i<=n;i++) Q[i].clear(),E[i].clear(); for(k=1;k<=n;k++){ scanf("%d",&u); while(getchar()!='('); scanf("%d",&i); while(getchar()!=')'); for(j=1;j<=i;j++){ scanf("%d",&v); d[v]++; E[u].push_back(v); } } for(root=1;root<=n;root++) if(!d[root]) break; scanf("%d",&m); for(j=1;j<=m;j++){ while(getchar()!='('); scanf("%d%d",&u,&v); if(u==v) count[u]++; else{ Q[u].push_back(v); Q[v].push_back(u); } while(getchar()!=')'); } } int find_set(int s) { for(;s!=father[s];s=father[s]); return s; } void union_set(int u,int v) { int up=find_set(u); int vp=find_set(v); if(up!=vp) father[vp]=up; } void dfs(int s) { father[s]=s; anc[s]=s; for(int i=0;i<E[s].size();i++){ dfs(E[s][i]); union_set(s,E[s][i]); anc[find_set(s)]=s; } color[s]=1; for(int i=0;i<Q[s].size();i++){ if(color[Q[s][i]]) count[anc[find_set(Q[s][i])]]++; } } void show() { for(int i=1;i<=n;i++) if(count[i]) printf("%d:%d\n",i,count[i]); } int main() { // freopen("data.txt","r",stdin); while(scanf("%d",&n)!=EOF){ InitData(); dfs(root); show(); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator