QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#300577#7860. Graph of Maximum Degree 3willow#WA 6ms25800kbC++143.4kb2024-01-08 14:37:522024-01-08 14:37:53

Judging History

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

  • [2024-01-08 14:37:53]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:25800kb
  • [2024-01-08 14:37:52]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 100005;

namespace ModCalculator {
    const int MOD = 998244353;
    inline void Inc(int &x, int y) {
        x += y; if (x >= MOD) x -= MOD;
    }
    inline void Dec(int &x, int y) {
        x -= y; if (x < 0) x += MOD;
    }
    inline int Mul(int x, int y) {
        return 1LL * x * y % MOD;
    }
    inline int Ksm(int x, int y) {
        int ret = 1;
        for (; y; y >>= 1) {
            if (y & 1) ret = Mul(ret, x);
            x = Mul(x, x);
        }
        return ret;
    }
    inline int Inv(int x) {
        return Ksm(x, MOD - 2);
    }
}
using namespace ModCalculator;

int n, m;
set<int> red[MAXN], blue[MAXN], both[MAXN];
vector<int> g[MAXN], gg[2][MAXN];
int vis[2][MAXN];

void Add(int u, int v) {
    if (red[u].count(v)) {
        gg[0][u].push_back(v);
        gg[0][v].push_back(u);
    }
    if (blue[u].count(v)) {
        gg[1][u].push_back(v);
        gg[1][v].push_back(u);
    }
}

void DFS(int u, int c) {
    vis[c][u] = 1;
    for (auto v : gg[c][u]) {
        if (vis[c][v]) continue;
        DFS(v, c);
    }
}

int Check(int u) {
    vector<int> a = g[u];
    a.push_back(u);
    for (int i = 0; i < 4; ++i) {
        for (int j = i + 1; j < 4; ++j) {
            Add(a[i], a[j]);
        }
    }
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 4; ++j) {
            vis[i][a[j]] = 0;
        }
    }
    DFS(u, 0);
    DFS(u, 1);
    for (int i = 0; i < 4; ++i) {
        gg[0][a[i]].clear();
        gg[1][a[i]].clear();
    }
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 4; ++j) {
            if (!vis[i][a[j]]) return 0;
        }
    }
    return 1;
}

pair<int, int> Other(int u, int v) {
    for (auto x : red[u]) {
        if (x != v) {
            return make_pair(x, 0);
        }
    }
    for (auto x : blue[u]) {
        if (x != v) {
            return make_pair(x, 1);
        }
    }
    return make_pair(0, 0);
}

void solve() {
    scanf("%d %d", &n, &m);
    for (int i = 1, u, v, c; i <= m; ++i) {
        scanf("%d %d %d", &u, &v, &c);
        g[u].push_back(v);
        g[v].push_back(u);
        if (u > v) swap(u, v);
        if (c == 0) {
            red[u].insert(v);
            red[v].insert(u);
            if (blue[u].count(v)) {
                both[u].insert(v);
            }
        } else {
            blue[u].insert(v);
            blue[v].insert(u);
            if (red[u].count(v)) {
                both[u].insert(v);
            }
        }
    }
    int ans = n;
    for (int u = 1; u <= n; ++u) {
        Inc(ans, (int)both[u].size());
        for (auto v : both[u]) {
            pair<int, int> e1 = Other(u, v), e2 = Other(v, u);
            int x = e1.first, y = e2.first;
            if (!x || !y || e1.second == e2.second) continue;
            if (x == y) {
                Inc(ans, 1);
            } else {
                if (x > y) swap(x, y);
                if (u > x) continue;
                if (both[x].count(y)) {
                    Inc(ans, 1);
                }
            }
        }
    }
    for (int u = 1; u <= n; ++u) {
        if (g[u].size() == 3 && u < g[u][0] && u < g[u][1] && u < g[u][2]) {
            Inc(ans, Check(u));
        }
    }
    printf("%d\n", ans);
}

int main() {
    solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 24928kb

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

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: 6ms
memory: 24904kb

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:

28

result:

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