이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
#include "towns.h"
int dist[201][201];
vector<pair<int,int>> adj[301];
int pa=-1;
int n;
vector<pair<int,int>> pp;
vector<pair<int,int>> pp2;
void dfs(int no,int par=-1){
if(no==pa){
pp2=pp;
}
for(auto j:adj[no]){
if(j.a!=par){
pp.pb({no,j.b});
dfs(j.a,no);
pp.pop_back();
}
}
}
int maa=0;
int kk=0;
int ss[301];
void dfs2(int no,int par=-1,int lev=0){
ss[no]=0;
if(adj[no].size()==1){
ss[no]++;
}
maa=max(maa,lev);
for(auto j:adj[no]){
if(j.a!=par){
dfs2(j.a,no,lev+j.b);
ss[no]+=ss[j.a];
if(ss[j.a]>(n/2)){
kk=-1;
}
}
}
}
int ans=1e9;
int st=-1;
pair<int,int> ma={-1,-1};
void solve(int l,int r){
for(int i=0;i<2*n;i++){
adj[i].clear();
}
adj[l].pb({r,dist[l][r]});
adj[r].pb({l,dist[l][r]});
int ind2=n;
for(int i=0;i<n;i++){
int mi=-1;
if(l==i or r==i){
continue;
}
pair<int,int> cur={l,r};
/* for(int j=0;j<i;j++){
for(int k=j+1;k<i;k++){
int xx=dist[i][j]+dist[i][k]-dist[j][k];
if(mi==-1){
mi=xx;
cur={j,k};
}
if(xx<mi){
mi=xx;
cur={j,k};
}
}
}*/
int xx=(dist[cur.a][i]+dist[cur.b][i]-dist[cur.a][cur.b])/2;
pa=cur.b;
pp.clear();
pp2.clear();
dfs(cur.a);
int ind=0;
int su=0;
pp2.pb({pa,0});
while(ind+1<pp2.size()){
su+=pp2[ind].b;
ind++;
if(su>=dist[cur.a][i]-xx){
break;
}
}
//cout<<xx<<endl;
if(su==dist[cur.a][i]-xx){
ma=max(ma,{xx,i});
adj[pp2[ind].a].pb({i,xx});
adj[i].pb({pp2[ind].a,xx});
}
else{
pair<int,int> kk={pp2[ind-1].a,pp2[ind].a};
su-=pp2[ind-1].b;
int le=(dist[cur.a][i]-xx)-su;
vector<pair<int,int>> yy;
for(auto j:adj[kk.a]){
if(j.a!=kk.b){
yy.pb(j);
}
}
adj[kk.a]=yy;
yy.clear();
for(auto j:adj[kk.b]){
if(j.a!=kk.a){
yy.pb(j);
}
}
adj[kk.b]=yy;
adj[kk.a].pb({ind2,le});
adj[kk.b].pb({ind2,pp2[ind-1].b-le});
adj[ind2].pb({kk.a,le});
adj[ind2].pb({kk.b,pp2[ind-1].b-le});
adj[ind2].pb({i,xx});
adj[i].pb({ind2,xx});
ind2++;
}
}
for(int i=n;i<ind2;i++){
maa=0;
kk=1;
dfs2(i);
if(maa<ans){
ans=maa;
st=kk;
}
else if(maa==ans){
st=max(st,kk);
}
//ans=min(ans,ma);
}
}
int hubDistance(int nn, int sub) {
n=nn;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(i==0 or j==0 or i==1 or j==1){
dist[i][j]=getDistance(i,j);
dist[j][i]=dist[i][j];
}
}
}
for(int i=0;i<2*n;i++){
adj[i].clear();
}
adj[0].pb({1,dist[0][1]});
adj[1].pb({0,dist[0][1]});
/* vector<pair<int,int>> tt;
for(int i=2;i<n;i++){
int xx=dist[0][i]+dist[1][i]-dist[0][1];
xx/=2;
tt.pb({})
}*/
ans=1e9;
st=-1;
ma={-1,-1};
solve(0,1);
for(int i=0;i<n;i++){
if(i!=ma.b){
dist[ma.b][i]=getDistance(ma.b,i);
dist[i][ma.b]=dist[ma.b][i];
}
}
solve(0,ma.b);
//cout<<ind2<<endl;
/* for(int i=0;i<ind2;i++){
cout<<i<<":"<<endl;
for(auto j:adj[i]){
cout<<j.a<<","<<j.b<<endl;
}
cout<<endl;
}*/
return ans*st;
//return R;
}
컴파일 시 표준 에러 (stderr) 메시지
towns.cpp: In function 'void solve(int, int)':
towns.cpp:88:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | while(ind+1<pp2.size()){
| ~~~~~^~~~~~~~~~~
towns.cpp:102:18: warning: declaration of 'kk' shadows a global declaration [-Wshadow]
102 | pair<int,int> kk={pp2[ind-1].a,pp2[ind].a};
| ^~
towns.cpp:32:5: note: shadowed declaration is here
32 | int kk=0;
| ^~
towns.cpp:62:7: warning: unused variable 'mi' [-Wunused-variable]
62 | int mi=-1;
| ^~
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:148:29: warning: unused parameter 'sub' [-Wunused-parameter]
148 | int hubDistance(int nn, int sub) {
| ~~~~^~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |