Codeforces 1217 D. Coloring Edges

题目链接:https://codeforces.com/contest/1217/problem/D

题意:n个点m条边的有向图,没有重边,没有自环,用尽可能少的颜色给每个边涂色,

使得所有环内的颜色不能为同一个颜色,这里的环是循环的环,即环上的每个点都能走到环上任意一点。

题解:先判断是否存在环,直接用拓朴排序就行,对于能出现环的,只会存在两种边,小顶点指向大顶点,和大顶点指向小顶点,那么每一种分一种颜色即可,不能出现环,那就每条边是一种颜色就可以。

代码:

# define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize(2)
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define int long long
#define fir first
#define sec second
#define IOS ios_base::sync_with_stdio(0);cin.tie(0)
using namespace std;
/*---------------------------------------------------------------------------------------------------------------------------*/
const int N = 5000 + 5;
const double pi = acos(-1.0);
typedef long long ll;
const int mod = 998244353;
//const int mod = 1e9 + 7;
#define inf 0x3f3f3f3f
struct something {
  int num, period, time;
  bool operator < (const something& y)const {
    return y.time < time || (y.time == time && y.num < num);
  }
  something() {}
  something(int num, int period, int time) :num(num), period(period), time(time) {}
};

int dx[8] = { 0, 0, 1,-1, 1, 1,-1,-1 };
int dy[8] = { 1,-1, 0, 0, 1,-1, 1,-1 };

int ver[N], Next[N], head[N], deg[N], tot;
void add(int x, int y) {
  ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
  deg[y]++;
}
bool topsort(int n) {
  queueq;
  vectorA;
  for (int i = 1; i <= n; i++)
    if (deg[i] == 0)q.push(i);
  while (q.size()) {
    int x = q.front(); q.pop();
    A.push_back(x);
    for (int i = head[x]; i; i = Next[i]) {
      int y = ver[i];
      if (--deg[y] == 0)q.push(y);
    }
  }
  return A.size() != n;
}
signed main() {
  IOS;
#ifdef ONLINE_JUDGE
#else
  freopen("Data.txt", "r", stdin);
#endif

  int n, m;
  while (cin >> n >> m) {
    vectorans;
    for (int i = 0; i < m; i++) {
      int x, y;
      cin >> x >> y;
      add(x, y);
      if (x > y)ans.push_back(1);
      else ans.push_back(2);
    }

    if (topsort(n)) {
      cout << 2 << endl;
      for (auto it : ans) cout << it << " ";
      cout << endl;
    }
    else {
      cout << 1 << endl;
      for (int i = 0; i < m; i++)
        cout << 1 << " ";
      cout << endl;
    }
  }


}

题目描述:

You are given a directed graph with nn vertices and mm directed edges without self-loops or multiple edges.

Let's denote the kk-coloring of a digraph as following: you color each edge in one of kk colors. The kk-coloring is good if and only if there no cycle formed by edges of same color.

Find a good kk-coloring of given digraph with minimum possible kk.

Input

The first line contains two integers nn and mm (2≤n≤50002≤n≤5000, 1≤m≤50001≤m≤5000) — the number of vertices and edges in the digraph, respectively.

Next mm lines contain description of edges — one per line. Each edge is a pair of integers uu and vv (1≤u,v≤n1≤u,v≤n, u≠vu≠v) — there is directed edge from uu to vv in the graph.

It is guaranteed that each ordered pair (u,v)(u,v) appears in the list of edges at most once.

Output

In the first line print single integer kk — the number of used colors in a good kk-coloring of given graph.

In the second line print mm integers c1,c2,…,cmc1,c2,…,cm (1≤ci≤k1≤ci≤k), where cici is a color of the ii-th edge (in order as they are given in the input).

If there are multiple answers print any of them (you still have to minimize kk).

你可能感兴趣的:(0x25广度优先搜索,Codeforces)