sicily 1034

    技术2022-06-28  42

    #include <iostream> #include <cstring> #include <queue> using namespace std; struct node { int level; int num; node(int num, int level) { this->num = num; this->level = level; } }; int n, m; bool isVis[101]; bool adj[101][101]; bool inValid; int degree[101]; int width[101]; int level; int main() { while(cin >> n >> m && n != 0) { memset(isVis, false, sizeof(isVis)); memset(adj, false, sizeof(adj)); memset(degree, 0, sizeof(degree)); memset(width, 0, sizeof(width)); inValid = false; level = 0; for(int i = 1; i <= m; i++) { int a, b; cin >> a >> b; adj[a][b] = true; degree[b]++; if(a == b || degree[b] > 1) inValid = true; } if(inValid) { cout << "INVALID/n"; continue; } int sorted = 0; queue<node> q; for(int i = 1; i <= n; i++) { if(degree[i] == 0) { q.push(node(i, 0)); isVis[i] = true; degree[i] = -1; } } while(!q.empty()) { node tmp = q.front(); q.pop(); sorted++; width[tmp.level]++; if(level < tmp.level) level = tmp.level; for(int i = 1; i <= n; i++) { if(!isVis[i] && adj[tmp.num][i]) { degree[i]--; if(degree[i] == 0) { q.push(node(i, tmp.level + 1)); isVis[i] = true; degree[i] = -1; } } } } if(sorted == n) { cout << level << " "; int maxWidth = width[0]; for(int i = 1; i < n; i++) if(maxWidth < width[i]) maxWidth = width[i]; cout << maxWidth << endl; }else cout << "INVALID/n"; } return 0; } 


    最新回复(0)