Descriptionios
Input編程
Outputui
Sample Inputspa
4 3 1 2 2 3 4 3
Sample Outputcode
1 2 4 3放法一:我用鄰接矩陣寫的,看起來有點土,不過最高興的是我終於把本身的想法實現了,寫這一題要注意有重邊的情況,要否則會RW。劣碼以下:#include<iostream> #include<cstring> #include<queue> using namespace std; typedef struct dot{ int num; int du; bool falg; }team; #define MAX 550 int a[MAX][MAX] = {0} ; int main() { int n,m; while(cin>>n>>m) { int x,y; team k[MAX]={0}; memset(a,0,sizeof(a)); memset(k,0,sizeof(k)); for(int i = 0; i < m;i++){ cin>>x>>y; if(a[x][y] != 1) k[y].du ++ ; a[x][y] = 1; //cout<<y<<" "<<k[y].du <<endl; k[y].falg = 0; } queue<int> q; while(!q.empty())q.pop(); for(int i = 1; i <= n;i ++){ if(k[i].du == 0){ k[i].falg = 1; q.push(i); break; } } /* for(int i = 1;i <= n;i ++) cout<<k[i].du<<" "; cout<<endl; cout<<q.size()<<endl; */ int f = 0,goal[600]; while(!q.empty() ){ int temp; temp = q.front() ; goal[f++]=temp; // cout<<temp <<endl; q.pop(); for(int i = 1; i <= n; i ++){ if(a[temp ][i] == 1) k[i].du --; } for(int i = 1; i<= n; i ++){ if(k[i].du == 0&&k[i].falg == 0){ q.push(i); k[i].falg = 1; break; } } } for(int i = 0;i < f-1;i ++) cout<<goal[i]<<" "; cout<<goal[f-1]<<endl; } return 0; }
AC好開心!