题目链接:
/*Problem : 2647 ( Reward ) Judge Status : Accepted RunId : 16919085 Language : G++ Author : 2014300227 Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta*/ #include <bits/stdc++.h> using namespace std; const int N=1e4+6; typedef long long ll; const ll mod=1e9+7; int n,m,ind[N],u,v,vis[N]; vector<int>ve[N]; struct node { int num,pri; }; node temp; queue<node>qu; int main() { while(scanf("%d%d",&n,&m)!=EOF) { ll ans=0; for(int i=1;i<=n;i++) { ve[i].clear(); vis[i]=ind[i]=0; } for(int i=0;i<m;i++) { scanf("%d%d",&u,&v); ve[v].push_back(u); ind[u]++; } for(int i=1;i<=n;i++) { if(!ind[i]) { temp.num=i; temp.pri=888; qu.push(temp); } } int cnt=n; while(!qu.empty()) { node fr=qu.front(); ans+=(ll)fr.pri; qu.pop(); cnt--; int len=ve[fr.num].size(); for(int i=0;i<len;i++) { int y=ve[fr.num][i]; ind[y]--; if(ind[y]==0) { temp.num=y; temp.pri=fr.pri+1; qu.push(temp); } } } if(cnt==0)printf("%lld\n",ans); else printf("-1\n"); } return 0; }
/*Problem : 2094 ( 产生冠军 ) Judge Status : Accepted RunId : 16919272 Language : G++ Author : 2014300227 Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta*/ #include <bits/stdc++.h> using namespace std; const int N=1e4+6; typedef long long ll; const ll mod=1e9+7; int n,ind[N]; map<string,int>mp; vector<int>ve[N]; string str1[N],str2[N]; int main() { while(1) { scanf("%d",&n); if(n==0)break; for(int i=1;i<=n;i++) { ve[i].clear(); ind[i]=0; } int cnt=1; for(int i=0;i<n;i++) { cin>>str1[i]>>str2[i]; if(mp[str1[i]]==0)mp[str1[i]]=cnt++; if(mp[str2[i]]==0)mp[str2[i]]=cnt++; int u=mp[str1[i]],v=mp[str2[i]]; ve[u].push_back(v); ind[v]++; } int num=0; for(int i=1;i<cnt;i++) { if(!ind[i]) { num++; } } if(num!=1)printf("No\n"); else printf("Yes\n"); for(int i=0;i<n;i++) { mp[str1[i]]=0; mp[str2[i]]=0; } } return 0; }
/*Problem : 2647 ( Reward ) Judge Status : Accepted RunId : 16919085 Language : G++ Author : 2014300227 Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta*/ #include <bits/stdc++.h> using namespace std; const int N=1e4+6; typedef long long ll; const ll mod=1e9+7; int n,m,ind[N],u,v,vis[N]; vector<int>ve[N]; struct node { int num,pri; }; node temp; queue<node>qu; int main() { while(scanf("%d%d",&n,&m)!=EOF) { ll ans=0; for(int i=1;i<=n;i++) { ve[i].clear(); vis[i]=ind[i]=0; } for(int i=0;i<m;i++) { scanf("%d%d",&u,&v); ve[v].push_back(u); ind[u]++; } for(int i=1;i<=n;i++) { if(!ind[i]) { temp.num=i; temp.pri=888; qu.push(temp); } } int cnt=n; while(!qu.empty()) { node fr=qu.front(); ans+=(ll)fr.pri; qu.pop(); cnt--; int len=ve[fr.num].size(); for(int i=0;i<len;i++) { int y=ve[fr.num][i]; ind[y]--; if(ind[y]==0) { temp.num=y; temp.pri=fr.pri+1; qu.push(temp); } } } if(cnt==0)printf("%lld\n",ans); else printf("-1\n"); } return 0; }
时间: 2024-10-09 05:14:59