이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5e5;
const int MAXK = 5e5;
const int INF = 987654321;
int N, K;
vector<int> adj[MAXN+10];
int P[MAXN+10];
int dep[MAXN+10], par[MAXN+10];
int dp[MAXN+10], dp2[MAXN+10];
void dfs(int now, int bef, int d)
{
dep[now]=d;
par[now]=bef;
dp[now]=INF;
for(int nxt : adj[now])
{
if(nxt==bef) continue;
dfs(nxt, now, d+1);
dp[now]=min(dp[now], dp[nxt]+1);
}
if(P[now]) dp[now]=0;
}
void dfs2(int now, int bef)
{
int p=INF;
for(int i=0; i<adj[now].size(); i++)
{
int nxt=adj[now][i];
if(nxt==bef) continue;
dp2[nxt]=min(dp2[nxt], p);
p=min(p, dp[nxt]+1);
}
p=INF;
for(int i=adj[now].size()-1; i>=0; i--)
{
int nxt=adj[now][i];
if(nxt==bef) continue;
dp2[nxt]=min(dp2[nxt], p);
p=min(p, dp[nxt]+1);
}
for(int nxt : adj[now])
{
if(nxt==bef) continue;
dp2[nxt]=min(dp2[nxt], dp2[now]+1);
}
if(P[now])
{
for(int nxt : adj[now])
{
if(nxt==bef) continue;
dp2[nxt]=0;
}
}
for(int nxt : adj[now])
{
if(nxt==bef) continue;
dfs2(nxt, now);
}
}
struct Data
{
int u, du, dv;
bool operator < (const Data &p) const
{
return du<p.du;
}
};
int L[MAXN+10];
bool vis[MAXN+10];
int main()
{
scanf("%d%d", &N, &K);
for(int i=1; i<N; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> VV;
for(int i=0; i<K; i++)
{
int t;
scanf("%d", &t);
VV.push_back(t);
P[t]=1;
}
dfs(1, 1, 1);
for(int i=1; i<=N; i++) dp2[i]=INF;
dfs2(1, 1);
//for(int i=1; i<=N; i++) printf("%d ", dp2[i]); printf("\n");
priority_queue<Data> PQ;
for(auto it : VV)
{
PQ.push({it, dep[it], dep[it],});
vis[it]=true;
}
vector<int> ans;
memset(L, -1, sizeof(L));
while(!PQ.empty())
{
Data now=PQ.top(); PQ.pop();
//printf("PQ %d %d %d %d\n", now.u, now.du, now.dv, L[now.u]);
if(now.dv-dep[now.u]==L[now.u]) continue;
if(now.u==1 || dp2[now.u]<now.dv-dep[par[now.u]])
{
ans.push_back(now.u);
//printf("FIX %d\n", now.u);
int p=now.u, k=now.dv-dep[now.u];
for(int i=k-1; i>=0; i--)
{
p=par[p];
//printf("%d %d\n", p, i);
L[p]=i;
if(p==1) break;
}
}
else if(!vis[par[now.u]])
{
vis[par[now.u]]=true;
PQ.push({par[now.u], dep[par[now.u]], now.dv});
}
}
printf("%d\n", ans.size());
for(auto it : ans) printf("%d ", it); printf("\n");
}
컴파일 시 표준 에러 (stderr) 메시지
pastiri.cpp: In function 'void dfs2(int, int)':
pastiri.cpp:37:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i=0; i<adj[now].size(); i++)
| ~^~~~~~~~~~~~~~~~
pastiri.cpp: In function 'int main()':
pastiri.cpp:141:11: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wformat=]
141 | printf("%d\n", ans.size());
| ~^ ~~~~~~~~~~
| | |
| int std::vector<int>::size_type {aka long unsigned int}
| %ld
pastiri.cpp:142:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
142 | for(auto it : ans) printf("%d ", it); printf("\n");
| ^~~
pastiri.cpp:142:40: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
142 | for(auto it : ans) printf("%d ", it); printf("\n");
| ^~~~~~
pastiri.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
86 | scanf("%d%d", &N, &K);
| ~~~~~^~~~~~~~~~~~~~~~
pastiri.cpp:90:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
90 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
pastiri.cpp:98:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
98 | scanf("%d", &t);
| ~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |