QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#671101#6830. Just Some Bad MemoryXiaoretaWRE 0ms3708kbC++202.5kb2024-10-24 10:54:132024-10-24 10:54:16

Judging History

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

  • [2024-10-24 10:54:16]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3708kb
  • [2024-10-24 10:54:13]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define all(a) a.begin(), a.end()
#define rep(i,a,b) for(int i = a; i < b; ++i)
#define per(i,b,a) for(int i = b-1; i >= a; --i)

#define debug(...) 42;
#ifdef LOCAL
#include "debug.h"
#endif

struct DSU {
    int n;
    vector<int> p, sz;
    DSU(int _n) {
        n = _n;
        p.resize(n);
        sz.resize(n, 1);
        iota(all(p), 0);
    }
    int leader(int u) { return u == p[u] ? u : p[u] = leader(p[u]); }
    bool merge(int u, int v) {
        int a = leader(u), b = leader(v);
        if (a != b) {
            // if (sz[a] < sz[b]) swap(a, b);
            p[b] = a;
            sz[a] += sz[b];
            return true;
        }
        return false;
    }
    int size(int u){
        return sz[leader(u)];
    }
};

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    int n, m; cin >> n >> m;
    vector<vi> adj(n+1);
    rep(i,0,m){
        int u, v; cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    if(n <= 3){
        cout << -1 << '\n';
        return 0;
    }

    map<pii, int> check;
    int odd = 0, even = 0;
    int idx = 0;
    vi color(n+1, -1), dfn(n+1);
    function<void(int, int)> dfs = [&](int u, int c){
        color[u] = c;
        dfn[u] = ++idx;
        for(int v: adj[u]) if(color[v] == -1){
            dfs(v, c^1);
        }else{
            if(color[v] == color[u]^1){
                assert((dfn[u] - dfn[v] + 1) % 2 == 0);
                if(dfn[u] - dfn[v] + 1 >= 4) even = 1;
            }else{
                odd = 1;
                if(check.count({u, v})) even = 1;
                check[{u, v}] = 1;
            }
        }
    }; rep(i,1,n+1) if(color[i] == -1) dfs(i, 0);

    DSU dsu(n+1);
    vector<int> comp(n+1);
    rep(u,1,n+1) for(int v: adj[u]) dsu.merge(u, v);
    rep(i,1,n+1) comp[dsu.leader(i)]++;

    int ans;
    if(even and odd){
        ans = 0;
    }
    if(even and !odd){
        ans = 1;
    }
    if(!even and odd){
        bool more = false;
        rep(i,1,n+1) if(comp[i] > 3) more = true;
        if(more) ans = 1;
        else ans = 2;
    }
    if(!even and !odd){
        sort(all(comp));
        int cur = 0;
        per(i,n+1,1){
            cur += comp[i];
            ans++;
            if(cur >= 4) break;
        }
        ans += 2-1;
    }
    cout << ans << '\n';

    return 0;
}

詳細信息

Test #1:

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

input:

3 3
1 2
2 3
1 3

output:

-1

result:

ok "-1"

Test #2:

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

input:

4 0

output:

5

result:

ok "5"

Test #3:

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

input:

5 4
1 2
2 3
3 4
4 5

output:

2

result:

ok "2"

Test #4:

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

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

input:

4 4
1 2
2 3
3 4
4 1

output:

1

result:

ok "1"

Test #6:

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

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: 0
Accepted
time: 0ms
memory: 3708kb

input:

4 3
1 2
2 3
3 1

output:

2

result:

ok "2"

Test #8:

score: -100
Runtime Error

input:

100000 99999
13413 22698
22698 36667
13413 64418
36667 75207
36667 73542
75207 91445
64418 3222
36667 96990
73542 61771
96990 33073
22698 32560
33073 24210
33073 38905
75207 46243
75207 89600
89600 11756
36667 94609
89600 6427
3222 46213
11756 43560
46243 50875
36667 45066
24210 54458
36667 80150
22...

output:


result: