# |
제출 시각 |
아이디 |
문제 |
언어 |
결과 |
실행 시간 |
메모리 |
412067 |
2021-05-26T13:12:47 Z |
반딧불(#7584) |
Pastiri (COI20_pastiri) |
C++17 |
|
1000 ms |
113204 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, k;
int par[500002], LCADB[500002][20];
vector<int> link[500002];
int minDist[500002], depth[500002];
vector<int> loc;
int ans;
int ansX[500002], ansY[500002];
void dfs_first(int x, int p=-1){
for(auto y: link[x]){
if(y == p) continue;
par[y] = x;
depth[y] = depth[x]+1;
dfs_first(y, x);
minDist[x] = min(minDist[x], minDist[y]+1);
}
}
void dfs_second(int x, int p=-1, int fromUp=1e9){
minDist[x] = min(minDist[x], fromUp);
for(auto y: link[x]){
if(y == p) continue;
dfs_second(y, x, minDist[x]+1);
}
}
int getLCA(int x, int y){
if(depth[x] > depth[y]) swap(x, y);
for(int d=0; d<20; d++) if((depth[y] - depth[x]) & (1<<d)) y = LCADB[y][d];
if(x==y) return x;
for(int d=19; d>=0; d--) if(LCADB[x][d] != LCADB[y][d]) x = LCADB[x][d], y = LCADB[y][d];
return par[x];
}
int dist(int x, int y){
return depth[x] + depth[y] - depth[getLCA(x, y)] * 2;
}
int main(){
scanf("%d %d", &n, &k);
for(int i=1; i<n; i++){
int x, y;
scanf("%d %d", &x, &y);
link[x].push_back(y), link[y].push_back(x);
}
for(int i=1; i<=n; i++) minDist[i] = 1e9;
for(int i=1; i<=k; i++){
int x;
scanf("%d", &x);
minDist[x] = 0;
loc.push_back(x);
}
dfs_first(1);
dfs_second(1);
for(int i=1; i<=n; i++) LCADB[i][0] = par[i];
for(int d=1; d<20; d++) for(int i=1; i<=n; i++) LCADB[i][d] = LCADB[LCADB[i][d-1]][d-1];
sort(loc.begin(), loc.end(), [&](int x, int y){
return depth[x] > depth[y];
});
for(int i=0; i<k; i++){
int p = loc[i];
bool noNeed = 0;
for(int j=0; j<ans; j++){
if(ansY[j] == dist(ansX[j], p)){
noNeed = 1;
break;
}
}
if(noNeed) continue;
int tmp = p, tNew = par[p], cnt=1;
while(tNew){
if(minDist[tNew] == cnt){
tmp = tNew;
tNew = par[tNew];
cnt++;
}
else break;
}
ansX[ans] = tmp;
ansY[ans] = cnt-1;
ans++;
}
printf("%d\n", ans);
for(int i=0; i<ans; i++) printf("%d ", ansX[i]);
}
Compilation message
pastiri.cpp: In function 'int main()':
pastiri.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d %d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~
pastiri.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
pastiri.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
358 ms |
111744 KB |
Output is correct |
2 |
Correct |
388 ms |
111816 KB |
Output is correct |
3 |
Correct |
435 ms |
111824 KB |
Output is correct |
4 |
Execution timed out |
1083 ms |
113204 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12620 KB |
Output is correct |
2 |
Correct |
11 ms |
12620 KB |
Output is correct |
3 |
Correct |
637 ms |
79228 KB |
Output is correct |
4 |
Correct |
612 ms |
81680 KB |
Output is correct |
5 |
Correct |
754 ms |
79336 KB |
Output is correct |
6 |
Correct |
794 ms |
82816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12236 KB |
Output is correct |
2 |
Correct |
10 ms |
12348 KB |
Output is correct |
3 |
Correct |
22 ms |
12364 KB |
Output is correct |
4 |
Correct |
24 ms |
12308 KB |
Output is correct |
5 |
Correct |
21 ms |
12364 KB |
Output is correct |
6 |
Correct |
23 ms |
12364 KB |
Output is correct |
7 |
Correct |
9 ms |
12264 KB |
Output is correct |
8 |
Correct |
10 ms |
12236 KB |
Output is correct |
9 |
Correct |
10 ms |
12236 KB |
Output is correct |
10 |
Correct |
12 ms |
12328 KB |
Output is correct |
11 |
Correct |
9 ms |
12236 KB |
Output is correct |
12 |
Correct |
9 ms |
12236 KB |
Output is correct |
13 |
Correct |
16 ms |
12324 KB |
Output is correct |
14 |
Correct |
17 ms |
12332 KB |
Output is correct |
15 |
Correct |
20 ms |
12372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1092 ms |
73728 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |