标签:des style blog io ar color os sp java
10305 - Ordering Tasks
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed.
Input
The input will consist of several instances of the problem. Each instance begins with a line containing two integers, 1 <= n <= 100 andm. n is the number of tasks (numbered from 1 to n) and m is the number of direct precedence relations between tasks. After this, there will be m lines with two integers i and j, representing the fact that task i must be executed before task j. An instance with n = m = 0will finish the input.
Output
5 4
1 2
2 3
1 3
1 5
0 0
1 4 2 5 3裸的,输出顺序任意,所以DFS做法可以求得。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> typedef long long LL; using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int maxn=110; int mp[maxn][maxn]; int toposort[maxn]; int vis[maxn]; int n,m,cnt; void dfs(int x) { vis[x]=1; for(int i=1;i<=n;i++) if(!vis[i]&&mp[x][i]&&i!=x) dfs(i); toposort[cnt++]=x; } void solve() { CLEAR(vis,0); for(int i=1;i<=n;i++) if(!vis[i]) dfs(i); } int main() { int x,y; std::ios::sync_with_stdio(false); while(cin>>n>>m&&(n+m)) { CLEAR(mp,0); while(m--) { cin>>x>>y; mp[x][y]=1; } cnt=0; solve(); for(int i=cnt-1;i>=0;i--) printf(i==0?"%d\n":"%d ",toposort[i]); } return 0; }HDU 1285
4 3 1 2 2 3 4 3
1 2 4 3
字典序最小的。。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> typedef long long LL; using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int maxn=550; int mp[maxn][maxn],vis[maxn]; int in[maxn],que[maxn]; int n,m,k; void toposort() { k=0;int i; while(k<n) { for(i=1;i<=n;i++) { if(!vis[i]&&!in[i]) { que[++k]=i; vis[i]=1; break; } } for(int j=1;j<=n;j++) { if(mp[i][j]) --in[j]; } } } void output() { for(int i=1;i<=k;i++) printf(i==k?"%d\n":"%d ",que[i]); } int main() { int x,y; std::ios::sync_with_stdio(false); while(~scanf("%d%d",&n,&m)) { CLEAR(in,0); CLEAR(mp,0); CLEAR(vis,0); while(m--) { scanf("%d%d",&x,&y); if(!mp[x][y]) { mp[x][y]=1; in[y]++; } } toposort(); output(); } return 0; }
3 2 0 1 1 2 2 2 0 1 1 0 0 0
YES NO
判断能不能拓扑排序。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> typedef long long LL; using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int maxn=110; int mp[maxn][maxn],vis[maxn]; int in[maxn]; int n,m,k; int toposort() { k=0;int i; for(int t=0;t<n;t++) { for(i=0;i<n;i++) { if(!vis[i]&&!in[i]) { vis[i]=1; in[i]--; k++; for(int j=0;j<n;j++) if(mp[i][j]) in[j]--; break; } } } // cout<<"2333 "<<k<<endl; return k==n?1:0; } int main() { int x,y; std::ios::sync_with_stdio(false); while(cin>>n>>m&&(n+m)) { CLEAR(mp,0); CLEAR(vis,0); CLEAR(in,0); while(m--) { cin>>x>>y; if(!mp[x][y]) { mp[x][y]=1; in[y]++; } } if(toposort()) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
标签:des style blog io ar color os sp java
原文地址:http://blog.csdn.net/u013582254/article/details/41710163