QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#312033#6329. Colorful GraphPlentyOfPenalty#WA 5ms5068kbC++202.8kb2024-01-23 11:07:272024-01-23 11:07:28

Judging History

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

  • [2024-01-23 11:07:28]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:5068kb
  • [2024-01-23 11:07:27]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;
typedef std::pair<int, int> pii;
void umin(int &a, int t) {
    if (t < a) a = t;
}
const int MAXN = 20011;
set<pii> S;
struct edge {
    int v, nxt;
} e[MAXN << 1 | 1];
int cnt = 1, last[MAXN];
void adde(int u, int v) { e[++cnt].v = v, e[cnt].nxt = last[u], last[u] = cnt; }

int dfn[MAXN], low[MAXN], cur = 0;
int s[MAXN], top = 0;
vector<int> seq[MAXN];
int scc[MAXN], scnt = 0;
bool ins[MAXN];
void tarjan(int u) {
    dfn[u] = low[u] = ++cur;
    s[++top] = u, ins[u] = 1;
    for (int i = last[u]; i; i = e[i].nxt) {
        int v = e[i].v;
        if (!dfn[v])
            tarjan(v), umin(low[u], low[v]);
        else if (ins[v])
            umin(low[u], dfn[v]);
    }
    if (dfn[u] == low[u]) {
        ++scnt;
        while (1) {
            int x = s[top--];
            seq[scnt].emplace_back(x);
            scc[x] = scnt;
            ins[x] = 0;
            if (x == u) break;
        }
    }
}
vector<int> g[MAXN], h[MAXN];
int match[MAXN];
bool vis[MAXN];
bool dfs(int u) {
    for (auto v : g[u])
        if (!vis[v]) {
            vis[v] = 1;
            if (!match[v] || dfs(match[v])) {
                match[v] = u;
                return 1;
            }
        }
    return 0;
}
int color[MAXN], cc = 0;
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        int u, v;
        cin >> u >> v;
        adde(u, v);
    }
    for (int u = 1; u <= n; ++u)
        if (!dfn[u]) tarjan(u);
    for (int u = 1; u <= n; ++u)
        for (int i = last[u]; i; i = e[i].nxt) {
            int v = e[i].v, p = scc[u], q = scc[v];
            if (p != q && !S.count(pii(p, q)) && !S.count(pii(q, p))) S.insert(pii(p, q)), g[p].emplace_back(q);
            // if (scc[u] != scc[v]) g[scc[u]].emplace_back(scc[v]); //, printf("add %d %d\n", scc[u], scc[v]);
        }
    // for (int u = 1; u <= n; ++u)
    // printf("scc[%d]=%d\n", u, scc[u]);
    int ans = 0;
    for (int u = 1; u <= scnt; ++u) {
        for (int i = 1; i <= scnt; ++i)
            vis[i] = 0;
        ans += dfs(u);
    }
    cerr << "ans=" << scnt - ans << '\n';
    // cout << scnt - ans << "\n";
    //  for (int u = 1; u <= scnt; ++u)
    //      printf("Match[%d]=%d\n", u, match[u]);
    for (int u = 1; u <= scnt; ++u)
        if (!color[seq[u][0]]) {
            ++cc;
            int p = u;
            while (p) {
                // printf("p=%d:", p);
                for (auto x : seq[p])
                    color[x] = cc; //, printf("color[%d]=%d\n", x, color[x]);
                p = match[p];
            }
        }
    for (int u = 1; u <= n; ++u)
        cout << color[u] << " ";
    cout << '\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1ms
memory: 3580kb

input:

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

output:

1 1 2 1 1 

result:

ok AC

Test #3:

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

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: 5068kb

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:

1 94 425 187 1751 1752 1751 386 641 246 1 1 1753 1 232 1754 1235 1755 690 1726 494 1166 1751 253 89 1756 1757 1755 347 1758 580 553 1759 1760 394 393 1465 1761 1751 1762 226 593 1763 1217 1764 1064 1765 1751 1766 1767 861 1768 459 1751 104 1564 1 1769 720 1770 559 1751 1751 1751 1593 1771 1751 1751 ...

result:

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