2015百度之星程序设计大赛 - 热身赛
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Harry and Magical Computer
Accepts: 182
Submissions: 653
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
有向图判环。拓扑排序搞一下即可。
1 #include <iostream> 2 #include <sstream> 3 #include <ios> 4 #include <iomanip> 5 #include <functional> 6 #include <algorithm> 7 #include <vector> 8 #include <string> 9 #include <list> 10 #include <queue> 11 #include <deque> 12 #include <stack> 13 #include <set> 14 #include <map> 15 #include <cstdio> 16 #include <cstdlib> 17 #include <cmath> 18 #include <cstring> 19 #include <climits> 20 #include <cctype> 21 using namespace std; 22 #define XINF INT_MAX 23 #define INF 0x3FFFFFFF 24 #define MP(X,Y) make_pair(X,Y) 25 #define PB(X) push_back(X) 26 #define REP(X,N) for(int X=0;X<N;X++) 27 #define REP2(X,L,R) for(int X=L;X<=R;X++) 28 #define DEP(X,R,L) for(int X=R;X>=L;X--) 29 #define CLR(A,X) memset(A,X,sizeof(A)) 30 #define IT iterator 31 typedef long long ll; 32 typedef pair<int,int> PII; 33 typedef vector<PII> VII; 34 typedef vector<int> VI; 35 vector<int>G[110]; 36 int deg[110]; 37 int main() 38 { 39 ios::sync_with_stdio(false); 40 int n,m; 41 while(cin>>n>>m){ 42 int u,v; 43 for(int i=0;i<n;i++)G[i].clear(); 44 for(int i=0;i<n;i++)deg[i]=0; 45 for(int i=0;i<m;i++){ 46 cin>>u>>v; 47 u--; 48 v--; 49 G[v].PB(u); 50 deg[u]++; 51 } 52 queue<int>q; 53 for(int i=0;i<n;i++){ 54 if(!deg[i])q.push(i); 55 } 56 int num=0; 57 while(!q.empty()){ 58 int x= q.front(); 59 q.pop(); 60 num++; 61 for(int i=0;i<G[x].size();i++){ 62 int v=G[x][i]; 63 deg[v]--; 64 if(deg[v]==0)q.push(v); 65 } 66 } 67 if(num==n)cout<<"YES"<<endl; 68 else cout<<"NO"<<endl; 69 70 } 71 return 0; 72 }
Have meal
Accepts: 256
Submissions: 523
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)