QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#335317#837. Giant Penguina_z_cWA 119ms11796kbC++203.6kb2024-02-23 09:29:142024-02-23 09:29:14

Judging History

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

  • [2024-02-23 09:29:14]
  • 评测
  • 测评结果:WA
  • 用时:119ms
  • 内存:11796kb
  • [2024-02-23 09:29:14]
  • 提交

answer

// Problem: G. Giant Penguin
// Contest: Codeforces - 300iq Contest 3
// URL: https://codeforces.com/gym/102538/problem/G
// Memory Limit: 512 MB
// Time Limit: 3000 ms

#include<bits/stdc++.h>
using namespace std;
const int maxK = 12;
const int maxn = 1e5 + 10;
const int inf = 1e9+114;
vector<int> G[maxn], T[maxn];
int tfa[maxn];
bool vis[maxn];
int siz[maxn];
int n, m, K, q;
void MakeTree(int u) {
    vis[u] = true;
    siz[u] = 1;
    for(int v : G[u]) {
        if(vis[v]) continue;
        T[u].emplace_back(v), T[v].emplace_back(u);
        MakeTree(v);
        siz[u] += siz[v];
    }
}
int totsize;
int FindRt(int u, int fa) {
    if(siz[u] * 2 < totsize) return -1;
    int rt = u;
    for(int v : T[u]) {
        if(v == fa || vis[v]) continue;
        int tmp = FindRt(v, u);
        if(~tmp) rt = tmp;
    }
    return rt;
}
int keyp[maxn][maxK];
int mcls[maxn][maxK];
int kcnt[maxn];
int col[maxn];
int dis[maxn];
class FromKey {
public: 
    int rt, kp, dis;
    /*
        wut is the root
        the order of the keypoint
        distance
    */
    FromKey(int rt = 0, int kp = 0, int dis = 0) : rt(rt), kp(kp), dis(dis) {}
};
vector<FromKey> kdis[maxn];
void FindKey(int u, int fa, int rt) {
    bool flag = false;
    for(int v : G[u]) {
        if(col[v] == rt) {
            flag = true;
            break;
        }
    }
    if(flag) keyp[rt][kcnt[rt]++] = u;
    for(int v : T[u]) {
        if(v == fa || vis[v]) continue;
        FindKey(v, u, rt);
    }
}
void Color(int u, int fa, int c) {
    col[u] = c;
    siz[u] = 1;
    for(int v : T[u]) {
        if(v == fa || vis[v]) continue;
        Color(v, u, c);
        siz[u] += siz[v];
    }
}
void SetVal(int u, int fa, int val) {
    dis[u] = val;
    for(int v : T[u]) {
        if(v == fa || vis[v]) continue;
        SetVal(v, u, val);
    }
}
void Bfs(int rt, int ord) {
    queue<int> q;
    q.emplace(keyp[rt][ord]);
    dis[keyp[rt][ord]] = 0;
    while(!q.empty()) {
        int u = q.front();q.pop();
        kdis[u].emplace_back(rt, ord, dis[u]);
        for(int v : G[u]) {
            if(col[v] != rt || dis[v] <= dis[u] + 1) continue;
            dis[v] = dis[u] + 1;
            q.emplace(v);
        }
    }
}
void TreeCut(int rt) {
    col[rt] = rt;
    keyp[rt][kcnt[rt]++] = rt;
    for(int v : T[rt]) {
        if(vis[v]) continue;
        FindKey(v, rt, rt);
        Color(v, rt, rt); 
    } 
    for(int i = 0; i < kcnt[rt]; i++) {
        SetVal(rt, 0, inf);
        Bfs(rt, i);
        mcls[rt][i] = inf;
    }
    vis[rt] = true;
    for(int v : T[rt]) {
        if(vis[v]) continue;
        totsize = siz[v];
        TreeCut(FindRt(v, 0));
    } 
}
void Init() {
    MakeTree(1);
    memset(vis, 0, sizeof(vis));
    totsize = n;
    TreeCut(FindRt(1, 0));
}
void Modify(int u) {
    for(auto cur : kdis[u]) {
        int rt = cur.rt, ord = cur.kp, dis = cur.dis;
        mcls[rt][ord] = min(mcls[rt][ord], dis);
    }
}
int Query(int u) {
    int res = inf;
    for(auto cur : kdis[u]) {
        int rt = cur.rt, ord = cur.kp, dis = cur.dis;
        res = min(res, mcls[rt][ord] + dis);
    }
    return res;
}
int main() {
    cin >> n >> m >> K;
    for(int i = 1, u, v; i <= m; i++) {
        cin >> u >> v;
        G[u].emplace_back(v), G[v].emplace_back(u);
    }
    Init();
    cin >> q;
    for(int i = 1; i <= q; i++) {
        int op, u;
        cin >> op >> u;
        if(op == 1) {
            Modify(u);
        } else {
            cout << Query(u) << '\n';
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5 4 0
1 2
2 3
3 4
4 5
7
1 1
1 5
2 1
2 2
2 3
2 4
2 5

output:

0
1
2
1
0

result:

ok 5 number(s): "0 1 2 1 0"

Test #2:

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

input:

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

output:

2
2

result:

ok 2 number(s): "2 2"

Test #3:

score: 0
Accepted
time: 60ms
memory: 7796kb

input:

100 99 0
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
52...

output:

99
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
99
98
97
9...

result:

ok 199968 numbers

Test #4:

score: -100
Wrong Answer
time: 119ms
memory: 9956kb

input:

100 99 0
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 57
1 58
1 59
1 60
1 61...

output:

1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
0
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
...

result:

wrong answer 29009th numbers differ - expected: '2', found: '0'