# Educational Codeforces Round 72 D. Coloring Edges（拓扑判环）

## 题目链接

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>

const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
const int inf = 1e7;
const long long onf = 1e18;
#define me(a, b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI 3.14159265358979323846
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
vector<int> maps[maxn];
int in[maxn];
int u[maxn], v[maxn];
bool vis[maxn];
int n, m;

bool check() {///拓扑判环
for (int i = 1; i <= n; i++) {
int temp = -1;
for (int j = 1; j <= n; j++)
if (!in[j] && !vis[j]) {
temp = j, vis[j] = 1;
break;
}
if (temp == -1)
return 0;
for (int j = 0; j < maps[temp].size(); j++)
in[maps[temp][j]]--;
}
return 1;
}

int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &u[i], &v[i]);
maps[u[i]].push_back(v[i]);
in[v[i]]++;
}
if (check()) {
puts("1");
for (int i = 1; i <= m; i++)
printf("1 ");
} else {
puts("2");
for (int i = 1; i <= m; i++) {
if (v[i] > u[i])
printf("1 ");
else
printf("2 ");
}
}
return 0;
}


09-06 509

09-06 376

09-08 96

09-10 307

09-06 105

09-19 45

09-06 44

09-09 42

02-18 31

09-16 24

#### Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

©️2020 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。