QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#115090#6329. Colorful GraphNTRLover#WA 5ms5420kbC++204.3kb2023-06-24 16:19:142023-06-24 16:19:18

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-24 16:19:18]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:5420kb
  • [2023-06-24 16:19:14]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
struct Tarjan_SCC {
  int n, edg_id, _t, sc_id;
  vector<vector<int>> E;
  // vector<pair<int, int>> edges;
  vector<int> dfn, low, in_scc;
  stack<int> st;
  vector<bool> in_st;
  Tarjan_SCC(int _n)
      : n(_n), E(_n), dfn(_n, -1), low(_n), in_scc(_n, -1), in_st(_n, false) {
    sc_id = edg_id = _t = 0;
  }
  void addEdge(int u, int v) {
    E[u].emplace_back(v);
    // E[v].emplace_back(edg_id);
    // edg_id++;
  }
  void dfs(int u) {
    low[u] = dfn[u] = _t++;
    st.emplace(u), in_st[u] = true;
    for (auto v : E[u]) {
      // auto v = edges[id].second;
      if (dfn[v] == -1) {
        dfs(v);
        low[u] = min(low[u], low[v]);
      } else if (in_st[v]) {
        low[u] = min(low[u], dfn[v]);
      }
    }
    if (dfn[u] == low[u]) {
      // vector<int> _scc;
      // _scc.clear();
      while (st.top() != u) {
        auto v = st.top();
        in_scc[v] = sc_id, in_st[v] = false;
        st.pop();
      }
      in_scc[u] = sc_id;
      // scc.emplace_back(_scc);
      sc_id++;
      st.pop(), in_st[u] = false;
    }
  }
  void solve() {
    for (int i = 0; i < n; ++i) {
      if (dfn[i] == -1) {
        dfs(i);
      }
    }
  }
};
template <class Flow> struct dinic {
  struct edge {
    int u, v;
    Flow f;
    edge(int _u, int _v, Flow _f) : u(_u), v(_v), f(_f){};
  };
  vector<vector<int>> E;
  vector<edge> edg;
  vector<int> d, cur;
  int n;
  dinic(int _n) : E(_n), d(_n), cur(_n), n(_n) {}
  void addEdge(int u, int v, Flow w) {
    int id = (int)edg.size();
    edg.emplace_back(u, v, w), edg.emplace_back(v, u, 0);
    E[u].emplace_back(id), E[v].emplace_back(id ^ 1);
  }
  auto dfs(int u, int t, Flow flow) {
    if (u == t)
      return flow;
    Flow sum = 0;
    while (cur[u] < (int)E[u].size()) {
      auto id = E[u][cur[u]];
      auto [_, v, f] = edg[id];
      auto c = min(flow, f);
      if (d[u] + 1 == d[v] && c > 0) {
        auto add = dfs(v, t, c);
        sum += add, flow -= add, edg[id].f -= add, edg[id ^ 1].f += add;
      }
      if (!flow)
        break;
      cur[u]++;
    }
    if (!sum)
      d[u] = -1;
    return sum;
  }
  bool level(int s, int t) {
    fill(d.begin(), d.end(), -1), fill(cur.begin(), cur.end(), 0);
    queue<int> Q;
    Q.emplace(s), d[s] = 0;
    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      for (auto id : E[u]) {
        auto v = edg[id].v;
        if (d[v] == -1 && edg[id].f != 0)
          Q.emplace(v), d[v] = d[u] + 1;
      }
    }
    return d[t] != -1;
  }
  auto solve(int s, int t) {
    Flow ans = 0;
    while (level(s, t))
      ans += dfs(s, t, 1e9);
    return ans;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  int n, m;
  cin >> n >> m;
  Tarjan_SCC G(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G.addEdge(u, v);
  }
  G.solve();
  int new_n = G.sc_id;
  dinic<int> H(new_n * 2 + 2);
  int S = new_n * 2, T = new_n * 2 + 1;
  // for (int i = 0; i < n; ++i) {
  //   cout << G.in_scc[i] << ' ';
  // }
  // cout << "\n";
  for (int u = 0; u < n; ++u) {
    for (auto v : G.E[u]) {
      int i = G.in_scc[u], j = G.in_scc[v];
      if (i != j) {
        H.addEdge(i + new_n, j, 1);
        // cout << i << " " << j << "\n";
      }
    }
  }
  int id = (int)H.edg.size();
  for (int i = 0; i < new_n; ++i) {
    H.addEdge(S, i + new_n, 1);
    H.addEdge(i, T, 1);
  }

  auto ans = H.solve(S, T);
  vector<int> col(new_n, -1);
  vector<vector<int>> E(new_n);
  vector<int> in(new_n);
  for (int i = 0; i < id; i += 2) {
    auto [u, v, f] = H.edg[i];
    u -= new_n;
    if (f == 0) {
      // connect u->v
      // u and v is in the same color
      E[u].emplace_back(v);
      in[v]++;
    }
  }
  function<void(int, int)> dfs = [&](int u, int c) {
    col[u] = c;
    for (auto v : E[u]) {
      dfs(v, c);
    }
  };
  int c = 0;
  for (int i = 0; i < new_n; ++i) {
    if (in[i] == 0) {
      dfs(i, c++);
    }
  }
  assert(c == new_n - ans);
  // cout << ans << '\n';
  // cout << new_n - ans << "\n";
  for (int i = 0; i < n; ++i) {
    cout << col[G.in_scc[i]] + 1 << ' ';
  }
  cout << "\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3500kb

input:

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

output:

1 2 2 1 1 

result:

ok AC

Test #2:

score: 0
Accepted
time: 1ms
memory: 3504kb

input:

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

output:

1 1 2 2 2 

result:

ok AC

Test #3:

score: 0
Accepted
time: 1ms
memory: 3528kb

input:

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

output:

1 1 1 1 2 1 3 4 

result:

ok AC

Test #4:

score: -100
Wrong Answer
time: 5ms
memory: 5420kb

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 1551 913 1363 1752 1751 1752 990 459 1259 1750 1750 1753 1750 1289 1754 725 1755 361 1711 760 591 1752 1247 1561 1756 1757 1755 1076 1758 579 633 1759 1760 976 976 1173 1761 1752 1762 1297 546 1763 696 1764 389 1765 1752 1766 1767 17 1768 836 1752 1527 1390 1750 1769 292 1770 619 1752 1752 1752...

result:

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