QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#103047#6329. Colorful GraphAlpha_Q#WA 293ms10908kbC++172.2kb2023-05-04 06:03:352023-05-04 06:03:38

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-04 06:03:38]
  • 评测
  • 测评结果:WA
  • 用时:293ms
  • 内存:10908kb
  • [2023-05-04 06:03:35]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 7002;
const int INF = 1e8 + 5;

bitset <N> adj[N];
int m, match[N], dist[N];

bool bfs() {
  queue <int> q; 
  for (int i = 1; i <= m; ++i) {
    if (!match[i]) dist[i] = 0, q.emplace(i);
    else dist[i] = INF;
  } 
  dist[0] = INF;
  while (!q.empty()) {
    int u = q.front(); q.pop();
    if (!u) continue; 
    for (int v = 1; v <= m; ++v) if (adj[u][v]) {
      if (dist[match[v]] == INF) {
        dist[match[v]] = dist[u] + 1, 
        q.emplace(match[v]);
      }
    }
  } 
  return dist[0] != INF;    
}

bool dfs (int u) {
  if (!u) return 1; 
  for (int v = 1; v <= m; ++v) if (adj[u][v]) {
    if (dist[match[v]] == dist[u] + 1 and dfs(match[v])) {
      match[u] = v;
      return 1; 
    } 
  } 
  dist[u] = INF;
  return 0;
}

int hopcroftKarp() {
  int ret = 0; 
  while (bfs()) {
    for (int i = 1; i <= m; ++i) {
      ret += !match[i] and dfs(i);
    }
  }
  return ret;
}

bool vis[N];
int n, comp[N], root, in[N], ans[N];
vector <int> stk, g[N], h[N], dag[N];

void forw (int u) {
  vis[u] = 1;
  for (int v : g[u]) if (!vis[v]) forw(v);
  stk.emplace_back(u);
}

void bacw (int u) {
  comp[u] = m;
  for (int v : h[u]) if (!comp[v]) bacw(v);
}

void mark (int u) {
  adj[root][u] = 1;
  for (int v : dag[u]) if (!adj[root][v]) mark(v);
}

int main() {
  cin >> n >> m;
  while (m--) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].emplace_back(v);
    h[v].emplace_back(u);
  }
  for (int i = 1; i <= n; ++i) if (!vis[i]) forw(i);
  reverse(stk.begin(), stk.end()), m = 0;
  for (int i : stk) if (!comp[i]) ++m, bacw(i);
  for (int i = 1; i <= n; ++i) for (int j : g[i]) {
    if (comp[i] ^ comp[j]) dag[comp[i]].emplace_back(comp[j]);
  }
  for (int i = 1; i <= m; ++i) root = i, mark(i), adj[i][i] = 0;
  hopcroftKarp();
  for (int i = 1; i <= m; ++i) if (match[i]) ++in[match[i]];
  int color = 0;
  for (int i = 1; i <= m; ++i) if (!in[i]) {
    int at = i; ++color;
    while (at) ans[at] = color, at = match[at];
  }
  for (int i = 1; i <= n; ++i) printf("%d ", ans[comp[i]]);
  cout << '\n';
  return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4224kb

input:

5 5
1 4
2 3
1 3
2 5
5 1

output:

1 1 1 2 1 

result:

ok AC

Test #2:

score: 0
Accepted
time: 2ms
memory: 5740kb

input:

5 7
1 2
2 1
4 3
5 1
5 4
4 1
4 5

output:

2 2 1 1 1 

result:

ok AC

Test #3:

score: 0
Accepted
time: 0ms
memory: 4116kb

input:

8 6
6 1
3 4
3 6
2 3
4 1
6 4

output:

4 4 4 4 3 4 2 1 

result:

ok AC

Test #4:

score: -100
Wrong Answer
time: 293ms
memory: 10908kb

input:

7000 6999
4365 4296
2980 3141
6820 4995
4781 24
2416 5844
2940 2675
3293 2163
3853 5356
262 6706
1985 1497
5241 3803
353 1624
5838 4708
5452 3019
2029 6161
3849 4219
1095 1453
4268 4567
1184 1857
2911 3977
1662 2751
6353 6496
2002 6628
1407 4623
425 1331
4445 4277
1259 3165
4994 1044
2756 5788
5496 ...

output:

1750 1843 2174 1936 1748 1749 1748 2135 2390 1995 1750 1750 1747 1750 1981 1746 2984 1745 2439 3475 2243 2915 1749 2002 1838 1744 1743 1745 2096 1742 2329 2302 1741 1740 2143 2142 3214 1739 1749 1738 1975 2342 1737 2966 1736 2813 1735 1749 1734 1733 2610 1732 2208 1748 1853 3313 1750 1731 2469 1730 ...

result:

wrong answer Integer 1843 violates the range [1, 1750]