QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#357998#6526. CanvasPorNPtree#WA 9ms16892kbC++173.7kb2024-03-19 16:15:432024-03-19 16:15:43

Judging History

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

  • [2024-03-19 16:15:43]
  • 评测
  • 测评结果:WA
  • 用时:9ms
  • 内存:16892kb
  • [2024-03-19 16:15:43]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 5e5 + 5;

pair<int, int> E[N];
int a[N], b[N], vs[N], zz[N];
vector< pair<int, int> > G[N];
vector<int> z;

void dfs(int x)
{
    for (auto [v, w] : G[x]) {
        if (!vs[w]) {
            z.push_back(w);
            vs[w] = 1;
        }
        if (a[v] != 2) {
            a[v] = 2;
            dfs(v);
        }
    }
    return;
}

void dfs2(int x)
{
    for (auto [v, w] : G[x]) {
        if (!vs[w]) {
            z.push_back(w);
            vs[w] = 1;
            dfs2(v);
        }
    }
    return;
}

int dfn[N], low[N], inS[N], wh[N], du[N], id[N];
stack<int> S;

void tj(int x)
{
    dfn[x] = low[x] = ++dfn[0];
    inS[x] = 1;
    S.push(x);
    for (auto [v, w] : G[x]) {
        if (!dfn[v]) {
            tj(v);
            low[x] = min(low[x], low[v]);
        } else if (inS[v]) {
            low[x] = min(low[x], dfn[v]);
        }
    }
    if (dfn[x] == low[x]) {
        int v;
        ++wh[0];
        id[wh[0]] = x;
        do {
            v = S.top();
            S.pop();
            inS[v] = 0;
            wh[v] = wh[0];
        } while (v != x);
    }
    return;
}

signed main()
{
    int T;
    scanf("%d", &T);
    while (T--) {
        int n, m, q = 0;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; ++i) {
            a[i] = b[i] = 0;
            G[i].clear();
        }
        z.clear();
        vector<int> tz;
        for (int i = 1; i <= m; ++i) {
            vs[i] = 0;
            int l, x, r, y;
            scanf("%d%d%d%d", &l, &x, &r, &y);
            if (x == 1 && y == 1) {
                a[l] = a[r] = 1;
                tz.push_back(i);
                vs[i] = 1;
            } else if (x == 2 && y == 2) {
                b[l] = b[r] = 1;
                z.push_back(i);
            } else {
                E[++q] = make_pair((x == 1 ? l : r), (y == 1 ? l : r));
                zz[q] = i;
                G[(x == 1 ? l : r)].push_back(make_pair((y == 1 ? l : r), i));
                a[l] = a[r] = 1;
            }
        }
        for (int i = 1; i <= n; ++i) {
            if (b[i]) {
                a[i] = 2;
            }
        }
        for (int i = 1; i <= n; ++i) {
            if (a[i] == 2) {
                dfs(i);
            }
        }
        int res = 0;
        for (int i = 1; i <= n; ++i) {
            res += a[i];
            G[i].clear();
            dfn[i] = low[i] = 0;
        }
        for (int i = 1; i <= q; ++i) {
            if (a[E[i].first] == 1 && a[E[i].second] == 1 && !vs[i]) {
                G[E[i].first].push_back(make_pair(E[i].second, zz[i]));
            }
        }
        dfn[0] = wh[0] = 0;
        for (int i = 1; i <= n; ++i) {
            if (!dfn[i]) {
                tj(i);
            }
        }
        for (int i = 1; i <= wh[0]; ++i) {
            du[i] = 0;
        }
        for (int i = 1; i <= q; ++i) {
            if (a[E[i].first] == 1 && a[E[i].second] == 1 && !vs[i] && wh[E[i].first] != wh[E[i].second]) {
                ++du[wh[E[i].second]];
            }
        }
        res += n;
        for (int i = 1; i <= wh[0]; ++i) {
            if (!du[i]) {
                --res;
                dfs2(id[i]);
            }
        }
        for (int i = 0; i < (int)tz.size(); ++i) {
            z.push_back(tz[i]);
        }
        for (int i = 1; i <= m; ++i) {
            if (!vs[i]) {
                z.push_back(i);
            }
        }
        printf("%d\n", res);
        for (int i = m - 1; ~i; --i) {
            printf("%d%c", z[i], " \n"[!i]);
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 15824kb

input:

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

output:

7
2 4 1 3
5
2 1

result:

ok Correct. (2 test cases)

Test #2:

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

input:

1
10 13
1 1 2 2
2 1 3 2
1 2 3 1
3 1 4 2
4 1 5 2
5 1 6 2
4 2 6 1
7 1 8 2
8 1 9 2
7 2 9 1
5 2 9 1
8 2 10 2
1 1 10 1

output:

19
4 13 3 2 1 5 7 6 11 8 10 9 12

result:

ok Correct. (1 test case)

Test #3:

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

input:

1
7 5
2 1 6 2
1 2 6 1
1 1 5 1
2 2 7 1
1 1 7 2

output:

8
4 3 5 2 1

result:

ok Correct. (1 test case)

Test #4:

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

input:

1
7 6
2 1 7 2
2 1 4 2
1 2 4 1
2 1 6 1
1 1 6 2
2 2 6 1

output:

9
5 4 3 2 1 6

result:

ok Correct. (1 test case)

Test #5:

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

input:

1
7 5
5 2 7 1
5 1 6 2
3 2 7 1
3 2 6 1
6 1 7 2

output:

7
3 1 5 4 2

result:

ok Correct. (1 test case)

Test #6:

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

input:

1
7 6
1 2 5 1
2 1 7 2
1 2 7 1
2 2 7 1
1 1 5 2
1 2 3 1

output:

8
6 4 1 5 3 2

result:

ok Correct. (1 test case)

Test #7:

score: -100
Wrong Answer
time: 9ms
memory: 15632kb

input:

2000
15 16
2 2 3 1
12 2 15 1
3 2 9 1
6 2 14 1
2 1 15 2
5 2 6 1
7 1 10 1
9 2 15 1
2 2 3 1
4 2 12 1
2 2 9 1
5 2 8 2
3 2 13 1
12 1 13 2
9 2 13 1
5 1 14 2
15 15
5 2 11 1
1 2 8 1
8 1 15 2
6 2 8 2
8 2 9 1
1 1 6 2
6 1 9 2
2 2 5 1
2 1 10 2
7 2 10 1
1 1 15 2
5 2 15 1
7 1 11 2
1 1 2 1
5 2 9 1
15 14
3 1 5 2
1 ...

output:

23
8 7 11 3 15 9 1 13 14 10 2 5 6 4 16 12
20
14 3 12 11 6 2 1 13 10 9 8 15 5 7 4
21
2 14 11 13 6 10 3 9 12 7 8 4 1 5
18
5 3 2 7 11 1 13 4 8 12 6 10 14 2
21
18 15 9 7 3 2 1 6 10 13 11 14 16 12 5 17 8 4 15
21
1 3 2 11 7 12 13 8 9 14 5 4 10 1
20
10 8 5 3 1 2 11 9 7 15 6 13 4 12 10
19
14 9 3 11 2 5 16 1...

result:

wrong answer 9 is not in the permutation. (test case 4)