Topic links:
https://codeforces.com/contest/1217/problem/D
Meaning of the questions:
FIG contracted to $ k $ colors, the same color can not form a ring
data range:
$ 1 \ leq n \ leq $ 5,000
$ 1 \ leq m \ leq 5000 $
analysis:
Category talk:
1, there is no ring, coated with a clear color
2, there is a ring, then sure, each ring has at least one side of a large number of the node from a large number of the node to a small number of the node, a small number of the node to the edge
Then, a first edge to a color infected, infected with the second side of another color, the same color definitely not present to form a ring
AC Code:
#include<bits/stdc++.h> #define ll long long #define pii pair<int,int> using namespace std; const int maxn=5000+7; struct Edge{ int a,b,color; }edge[maxn]; int n,m,vis[maxn],now,k=1; vector<int>ve[maxn]; void dfs(int x){ if(vis[x]){ if(x==now)k=2; return ; } vis[x]=1; for(int i=0;i<ve[x].size();i++) dfs(ve[x][i]); } int main() { scanf("%d %d",&n,&m); for(int i=1;i<=m;i++){ int a,b; scanf("%d %d",&a,&b); edge[i]=(Edge){a,b,-1}; ve[a].push_back(b); } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++)vis[j]=0; now=i; dfs(i); } for(int i=1;i<=m;i++){ if(k==1||edge[i].a>edge[i].b)edge[i].color=1; else edge[i].color=2; } printf("%d\n",k); for(int i=1;i<=m;i++){ printf("%d",edge[i].color); if(i!=m)printf(" "); else printf("\n"); } return 0; }