이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
//#include "race.h"
using namespace std;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
struct cell
{
long long int a,b,c;
void read()
{
cin>>a>>b>>c;
}
};
long long int n,k,used[200005],dp[200005];
cell a[200005];
vector<cell>v[200005];
map<long long int,long long int>m,ne;
long long int otg;
void make_dp(long long int beg)
{
long long int w;
used[beg]=1;
dp[beg]=1;
for(long long int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(!used[w])
{
make_dp(w);
dp[beg]+=dp[w];
}
}
}
void cl(long long int beg,long long int par)
{
long long int w;
used[beg]=0;
dp[beg]=0;
for(long long int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(w!=par&&used[w]!=2)cl(w,beg);
}
}
void dfs(long long int beg,long long int st,long long int lead,long long int br)
{
if(st>k)return;
used[beg]=1;
cell w;
if(!ne[st])
{
ne[st]=br;
}
else ne[st]=min(ne[st],br);
for(long long int i=0;i<v[beg].size();i++)
{
w=v[beg][i];
if(!used[w.b])
{
dfs(w.b,st+w.c,lead,br+1);
}
if(beg==lead)
{
for(auto j=ne.begin();j!=ne.end();j++)
{
long long int h=j->first;
if(k==h)otg=min(otg,j->second);
if(m[k-h])
{
otg=min(otg,m[k-h]+j->second);
}
}
for(auto j=ne.begin();j!=ne.end();j++)
{
long long int h=j->first;
if(m[h])m[h]=min(m[h],ne[h]);
else m[h]=ne[h];
}
ne.clear();
}
}
}
void find_centroid(long long int beg,long long int par,long long int br)
{
long long int w,l=0;
used[beg]=1;
for(long long int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(!used[w]&&dp[w]>=(br+1)/2)
{
find_centroid(w,beg,br);
l=1;
}
}
if(!l)
{
cl(beg,beg);
dfs(beg,0,beg,0);
m.clear();
cl(beg,beg);
used[beg]=2;
for(long long int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(!used[w])
{
make_dp(w);
cl(w,w);
find_centroid(w,w,dp[w]);
}
}
}
}
int best_path(int N,int K,int H[][2],int L[])
//void read()
{
//cin>>n>>k;
n=N;
k=K;
for(long long int i=1;i<n;i++)
{
//cin>>a[i].a>>a[i].b;
a[i].a=H[i][0];
a[i].b=H[i][1];
}
for(long long int i=1;i<n;i++)
{
//cin>> a[i].c;
a[i].c=L[i];
v[a[i].a].push_back(a[i]);
swap(a[i].a,a[i].b);
v[a[i].a].push_back(a[i]);
}
//cout<<endl;
otg=1000000000;
make_dp(0);
cl(0,0);
find_centroid(0,0,dp[0]);
if(otg==1000000000)
{
//cout<<-1<<endl;
return -1;
}
return otg;
//cout<<otg<<endl;
}
/*int main()
{
speed();
read();
return 0;
}
*/
컴파일 시 표준 에러 (stderr) 메시지
race.cpp: In function 'void make_dp(long long int)':
race.cpp:28:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(long long int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp: In function 'void cl(long long int, long long int)':
race.cpp:43:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(long long int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp: In function 'void dfs(long long int, long long int, long long int, long long int)':
race.cpp:59:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(long long int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp: In function 'void find_centroid(long long int, long long int, long long int)':
race.cpp:91:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(long long int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp:107:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(long long int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
# | 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... |