QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#335203 | #837. Giant Penguin | sinsop90 | WA | 19ms | 13044kb | C++14 | 2.9kb | 2024-02-22 21:50:57 | 2024-02-22 21:50:58 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5, INF = 1e9;
int vis[maxn], n, SZ, rt, F[maxn], sz[maxn], head[maxn], tot, dis[maxn], visn[maxn], vism[maxn], m, K, QQ;
vector<int> vec[maxn], imp;
vector<pair<int, int>> tag[maxn];
queue<int> Q;
struct edge {
int v, pre;
}e[maxn << 1];
void add(int u, int v) {
e[++ tot].v = v;
e[tot].pre = head[u];
head[u] = tot;
}
void make_tree(int now) {
vis[now] = 1;
for(int i = head[now];i;i = e[i].pre) {
int v = e[i].v;
if(vis[v]) continue;
vec[now].push_back(v), vec[v].push_back(now);
make_tree(v);
}
}
void getSZ(int now, int f) {
SZ ++;
F[now] = sz[now] = 0;
for(int v : vec[now]) {
if(v == f || vis[v]) continue;
getSZ(v, now);
}
}
void getrt(int now, int f) {
sz[now] = 1;
for(int v : vec[now]) {
if(vis[v] || v == f) continue;
getrt(v, now);
sz[now] += sz[v];
F[now] = max(F[now], sz[v]);
}
F[now] = max(F[now], SZ - F[now]);
if(!rt || F[now] < F[rt]) rt = now;
}
void check(int now, int f, int id) {
visn[now] = id;
for(int v : vec[now]) {
if(v == f || vis[v]) continue;
check(v, now, id);
}
}
void getim(int now, int f) {
for(int i = head[now];i;i = e[i].pre) {
int v = e[i].v;
if(v == f || vis[v]) continue;
if(visn[v] > visn[now]) imp.push_back(now);
}
for(int v : vec[now]) {
if(v == f || vis[v]) continue;
getim(v, now);
}
}
void getdis(int now) {
// cout << now << " !!!!\n";
Q.push(now);
vism[now] = 1;
tag[now].push_back(make_pair(now, 0));
while(!Q.empty()) {
int t = Q.front();
Q.pop();
for(int i = head[now];i;i = e[i].pre) {
int v = e[i].v;
if(vism[v]) continue;
vism[v] = vism[now] + 1;
tag[v].push_back(make_pair(now, vism[v] - 1));
Q.push(v);
}
}
}
void cleartag(int now, int f) {
vism[now] = 0;
for(int v : vec[now]) {
if(v == f || vis[v]) continue;
cleartag(v, now);
}
}
void dfs(int now, int f) {
SZ = rt = 0;
getSZ(now, f);
getrt(now, f);
vis[rt] = 1;
// cout << rt << " " << F[rt] << " !!!" << '\n';
for(int v : vec[rt]) {
if(vis[v]) continue;
check(v, rt, v);
}
imp.clear();
for(int v : vec[rt]) {
if(vis[v]) continue;
getim(v, rt);
}
for(int x : imp) getdis(x), cleartag(rt, rt);
getdis(rt), cleartag(rt, rt);
for(int v : vec[rt]) {
if(vis[v]) continue;
dfs(v, now);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m >> K;
for(int i = 1, u, v;i <= m;i++) {
cin >> u >> v;
add(u, v), add(v, u);
}
make_tree(1);
for(int i = 1;i <= n;i++) vis[i] = 0, dis[i] = INF;
dfs(1, 0);
cin >> QQ;
for(int i = 1, op, x;i <= QQ;i++) {
cin >> op >> x;
if(op == 1) {
for(pair<int, int> t : tag[x]) dis[t.first] = min(dis[t.first], t.second);
}
else {
int ans = INF;
for(pair<int, int> t : tag[x]) ans = min(ans, t.second + dis[t.first]);
cout << ans << '\n';
}
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 5ms
memory: 13012kb
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: 13036kb
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: -100
Wrong Answer
time: 19ms
memory: 13044kb
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:
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 100...
result:
wrong answer 1st numbers differ - expected: '99', found: '1000000000'