QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#406727#6830. Just Some Bad Memoryjames1BadCreeperWA 2ms10668kbC++171.3kb2024-05-07 17:09:482024-05-07 17:11:23

Judging History

This is the latest submission verdict.

  • [2024-05-07 17:11:23]
  • Judged
  • Verdict: WA
  • Time: 2ms
  • Memory: 10668kb
  • [2024-05-07 17:09:48]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 200005; 

int n, m, ans, odd, even, chaingeq3;
int dep[N], sum[N], xx[N], yy[N];
vector<int> G[N];

void dfs(int x, int fa) {
    dep[x] = dep[fa] + 1;
    for (int y : G[x]) if (y != fa) {
        if (dep[y] == 0) { dfs(y, x); continue; }
        if ((dep[x] + dep[y]) & 1) even = 1; 
        else odd = 1; 
        if (dep[y] < dep[x]) ++sum[x], --sum[y]; 
    }
}
void calc(int x, int fa) {
    for (int y : G[x]) if (dep[y] == dep[x] + 1) calc(y, x), sum[x] += sum[y]; 
    if (sum[x] > 1) even = 1; 
}

int main(void) {
    cin >> n >> m; 
    if (n <= 3) return cout << "-1\n", 0; 
    for (int i = 1, x, y; i <= m; ++i)
        cin >> x >> y, G[x].emplace_back(y), G[y].emplace_back(x), xx[i] = x, yy[i] = y;
    for (int i = 1; i <= n; ++i) if (dep[i] == 0) dfs(i, 0), calc(i, 0); 
    if (m <= 2) return cout << 5 - m << "\n", 0; 
    if (odd && even) return cout << "0\n", 0; 
    for (int i = 1; i <= m && chaingeq3 == 0; ++i) {
        int x = xx[i], y = yy[i];
        if (G[x].size() > 1 && G[y].size() > 1)
            for (int a : G[x]) for (int b : G[y]) if (a != b && a != x && b != y)
                chaingeq3 = 1; 
    }
    if (even || (odd && chaingeq3)) return cout << "1\n", 0; 
    cout << "2\n"; 
    return 0; 
}

詳細信息

Test #1:

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

input:

3 3
1 2
2 3
1 3

output:

-1

result:

ok "-1"

Test #2:

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

input:

4 0

output:

5

result:

ok "5"

Test #3:

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

input:

5 4
1 2
2 3
3 4
4 5

output:

2

result:

ok "2"

Test #4:

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

input:

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

output:

0

result:

ok "0"

Test #5:

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

input:

4 4
1 2
2 3
3 4
4 1

output:

1

result:

ok "1"

Test #6:

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

input:

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

output:

0

result:

ok "0"

Test #7:

score: -100
Wrong Answer
time: 0ms
memory: 10008kb

input:

4 3
1 2
2 3
3 1

output:

1

result:

wrong answer 1st words differ - expected: '2', found: '1'