Atcoder ABC 139E
Meaning of the questions:
n large team round robin, each team one day only fight, seek at least a few days to finish.
solution:
Consider the abstract graph theory, since one day only play a game, then put every team played against the team and it needs even edge.
Obtaining topology sequence, topological sorting each degree from the point 0 and the answer is incremented by 1 by deleting a degree, this operation is repeated.
If the ring is formed, then no solution.
CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define N 1000010
#define M 1010
using namespace std;
struct Edge {
int from, to;
}e[N];
int n,num,cnt,tot,ans;
int head[N],a[M][M],node[M][M];
int inq[N],t[N],day[N];
inline void add_edge(int x,int y) {
e[++cnt].from = y;
e[cnt].to = head[x];
head[x] = cnt;
}
void topsort() {
for(int i = 1; i <= tot; i++) {
int u = t[i];
for(int j = head[u]; j; j = e[j].to) {
int v = e[j].from;
day[v] = day[u] + 1;
inq[v]--;
if(!inq[v])t[++tot] = v;
}
}
for(int i = 1; i <= num; i++)
ans = max(ans, day[i]);
}
int main() {
scanf("%d",&n);
for(int i = 1; i <= n; i++) {
for(int j = 1; j < n; j++) {
scanf("%d",&a[i][j]);
if(!node[i][j] && !node[j][i])
node[i][j] = node[j][i] = ++num;
}
}
for(int i = 1; i <= n; i++) {
for(int j = 2; j < n; j++) {
add_edge(node[i][a[i][j-1]], node[i][a[i][j]]);
inq[node[i][a[i][j]]]++;
}
}
for(int i = 1; i <= num; i++) {
if(!inq[i]) {
t[++tot] = i;
day[i] = 1;
}
}
topsort();
if(tot != num) puts("-1");
else printf("%d\n", ans);
//system("pause");
return 0;
}