QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#354211#7860. Graph of Maximum Degree 3ucup-team1198#WA 1ms3560kbC++202.9kb2024-03-14 23:40:312024-03-14 23:40:32

Judging History

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

  • [2024-03-14 23:40:32]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3560kb
  • [2024-03-14 23:40:31]
  • 提交

answer

#include <map>
#include <set>
#include <array>
#include <cmath>
#include <deque>
#include <bitset>
#include <random>
#include <string>
#include <vector>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

using namespace std;

const int MAXN = 100100;
vector<int> G[2][MAXN];

int timer[MAXN];
int par[MAXN];
int ttime = 0;

int get(int v) {
    if (v == par[v])
        return v;
    return par[v] = get(par[v]);
}

bool merge(int v, int u) {
    v = get(v);
    u = get(u);
    if (v == u)
        return false;
    par[v] = u;
    return true;
}

bool check_set(const vector<int>& vs) {
    ++ttime;
    for (int u : vs) {
        timer[u] = ttime;
    }
    for (int c = 0; c < 2; ++c) {
        for (int u : vs)
            par[u] = u;
        int comps = vs.size();
        for (int u : vs) {
            for (int v : G[c][u]) {
                if (timer[v] == ttime)
                    comps -= merge(v, u);
            }
        }
        if (comps != 1)
            return false;
    }
    return true;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        --a;
        --b;
        G[c][a].emplace_back(b);
        G[c][b].emplace_back(a);
    }
    int ans = n;
    vector<bool> is_good(n);
    for (int i = 0; i < n; ++i)
        is_good[i] = !G[0][i].empty() && !G[1][i].empty();
    vector<bool> was(n);
    vector<int> curr;
    for (int i = 0; i < n; ++i) {
        if (!is_good[i])
            continue;
        if (was[i])
            continue;
        vector<int> path;
        int cur = i;
        while (true) {
            path.emplace_back(cur);
            was[cur] = true;
            int to_next = -1;
            for (int nxt : G[0][cur]) {
                if (is_good[nxt] && !was[nxt]) {
                    to_next = nxt;
                    break;
                }
            }
            if (to_next == -1)
                break;
            cur = to_next;
        }
        bool is_cycle = false;
        for (int tmp : G[0][path.front()]) {
            if (tmp == path.back())
                is_cycle = true;
        }
        int look_mx = min(4, int(path.size() - 1));
        if (path.size() <= 4) {
            ans += check_set(path);
        }
        for (int j = 0; j < path.size(); ++j) {
            for (int l = 2; l <= look_mx; ++l) {
                if (j < l && !is_cycle)
                    continue;
                for (int k = 0; k < l; ++k)
                    curr.emplace_back(path[(j - k + path.size()) % path.size()]);
                ans += check_set(curr);
                curr.clear();
            }
        }
    }
    cout << ans << '\n';
    return 0;
}

詳細信息

Test #1:

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

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

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: -100
Wrong Answer
time: 1ms
memory: 3560kb

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:

25

result:

wrong answer 1st numbers differ - expected: '27', found: '25'