QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#306421#7860. Graph of Maximum Degree 3qilingWA 64ms16800kbC++232.5kb2024-01-16 19:07:292024-01-16 19:07:29

Judging History

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

  • [2024-01-16 19:07:29]
  • 评测
  • 测评结果:WA
  • 用时:64ms
  • 内存:16800kb
  • [2024-01-16 19:07:29]
  • 提交

answer

#include <bits/stdc++.h>

#define x first
#define N 100005
#define y second
#define mod 998244353
#define PII pair<int, int>

using namespace std;

void init(vector<int> &p, vector<int> &fa) {
    for (int i = 0; i < p.size(); ++ i) {
        fa[i] = i;
        p[i] = i;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    vector<int> p(n), fa(n), sz(n, 1), buk(n, 1);
    init(p, fa);
    vector<PII> edge[N];
    function<int(int)> _find = [&](int x) {
        if(p[x] == x) return x;
        return p[x] = _find(p[x]);
    };
    function<int(int)> find_ = [&](int x) {
        if(fa[x] == x) return x;
        return fa[x] = find_(fa[x]);
    };
    while(m --) {
        int u, v, c;
        cin >> u >> v >> c;
        -- u;
        -- v;
        
        edge[u].push_back({v, c});
        edge[v].push_back({u, c});
        if(_find(p[v]) != _find(p[u])) buk[p[v]] += buk[p[u]];
        p[u] = p[v];
        if(edge[u].size() > 1) {
            for (int i = 0; i < edge[u].size(); ++ i) {
                if(edge[u][i].x == v && edge[u][i].y == (1 ^ c)) {
                    sz[fa[v]] += sz[fa[u]];
                    fa[u] = find_(fa[v]);
                }
            }
        }
    }
    auto check = [=](int x) {
        set<int> S;
        function<void(int, int)> check_4 = [&](int x, int c) {
            if(S.find(x) != S.end()) return;
            S.insert(x);
            for (auto [u, w] : edge[x]) {
                if(w == c) check_4(u, c);
            }
        };
        check_4(x, 0);
        bool f = (S.size() == 4);
        S.clear();
        check_4(x, 1);
        return f && (S.size() == 4);
    };
    int ans = n;
    bool st[N] = {0};
    for (int i = 0; i < n; ++ i) {
        if(buk[_find(i)] == 4 && !st[_find(i)]) {
            if(check(i)) ++ ans;
            st[_find(i)] = true;
        }
        if(sz[find_(i)] == 2 && !st[fa[i]]) {
            ++ ans;
            int u = i, v;
            for(auto [x, y] : edge[u]) {
                if(find_(u) == find_(x)) {
                    v = x;
                    break;
                }
            }
            for (int j = 0; j < edge[u].size(); ++ j) {
                for (int k = 0; k < edge[v].size(); ++ k) {
                    if(edge[u][j].x == edge[v][k].x && edge[u][j].y != edge[v][k].y) ++ ans;
                }
            }
            st[fa[i]] = true;
        }
    }
    cout << ans << endl;
    return 0;
}
/*
 3 4
 1 2 0
 1 3 1
 2 3 0
 2 3 1
 4 6
 1 2 0
 2 3 0
 3 4 0
 1 4 1
 2 4 1
 1 3 1
 */

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 8548kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: 0
Accepted
time: 3ms
memory: 8372kb

input:

4 6
1 2 0
2 3 0
3 4 0
1 4 1
2 4 1
1 3 1

output:

5

result:

ok 1 number(s): "5"

Test #3:

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

input:

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

output:

27

result:

ok 1 number(s): "27"

Test #4:

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

input:

100 150
93 23 0
23 81 0
76 81 0
93 81 1
93 76 1
23 76 1
100 65 0
65 56 0
19 56 0
100 56 1
100 19 1
65 19 1
2 98 0
2 98 1
26 63 0
63 90 0
26 63 1
26 90 1
6 11 0
11 67 0
6 11 1
6 67 1
37 89 0
89 64 0
25 64 0
37 64 1
37 25 1
89 25 1
84 10 0
10 29 0
75 29 0
84 29 1
84 75 1
10 75 1
7 70 1
7 70 0
28 92 0
...

output:

141

result:

ok 1 number(s): "141"

Test #5:

score: -100
Wrong Answer
time: 64ms
memory: 16800kb

input:

100000 133680
36843 86625 0
86625 63051 0
35524 63051 0
36843 63051 1
36843 35524 1
86625 35524 1
55797 82715 0
55797 82715 1
70147 35104 0
35104 91732 0
70147 35104 1
70147 91732 1
94917 70395 0
70395 68250 0
24100 68250 0
94917 68250 1
94917 24100 1
70395 24100 1
83033 18450 1
83033 18450 0
34462 ...

output:

144402

result:

wrong answer 1st numbers differ - expected: '144604', found: '144402'