답안 #593240

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
593240 2022-07-10T15:52:00 Z chirathnirodha 경주 (Race) (IOI11_race) C++17
100 / 100
2775 ms 80780 KB
#include "race.h"
#include<bits/stdc++.h>
using namespace std;
#define PB push_back
#define MP make_pair
#define F first
#define S second 
#define P push
#define I insert
typedef long long ll;
const ll INF=10000000000000;
const int maxn=200000;
ll n,k;
int ans=INT32_MAX;
vector<pair<ll,ll> > v[maxn];
int parent[maxn];
int subsiz[maxn];
int len[maxn];
ll dist[maxn];
int dfs(int x,set<int> s){
  subsiz[x]=1;
  for(int i=0;i<v[x].size();i++){
    int y=v[x][i].F;
    if(parent[x]==y || s.count(y))continue;
    parent[y]=x;
    dist[y]=dist[x]+v[x][i].S;
    len[y]=len[x]+1;
    subsiz[x]+=dfs(y,s);
  }
  return subsiz[x];
}
int find_centroid(int x,int parsub,set<int> s,int gsiz){
  pair<ll,int> p={-1,-1};
  for(int i=0;i<v[x].size();i++){
    int y=v[x][i].F;
    if(parent[x]==y || s.count(y))continue;
    if(p.F<subsiz[y])p={subsiz[y],y};
  }
  if(p.F<=gsiz/2)return x;
  else return find_centroid(p.S,subsiz[x]+parsub-p.F,s,gsiz);
}
map<ll,int> func(int x,int prcen,set<int> s){
  parent[x]=prcen;len[x]=1;dist[x]=0;
  dfs(x,s);
  int cen=find_centroid(x,0,s,subsiz[x]);
  ll cenpar=parent[cen],cenparlen;
  map<ll,int> mp,mp1;mp[0]=1;
 
  s.I(cen);
 
  ll y,disty;
  for(int i=0;i<v[cen].size();i++){
    y=v[cen][i].F;
    disty=v[cen][i].S;
 	
    if(y==cenpar){
      cenparlen=disty;
      continue;
    }
 	if(s.count(y))continue;
    mp1=func(y,cen,s);
    for(auto itr=mp1.begin();itr!=mp1.end();itr++){
      pair<int,int> p=*itr;
      if(mp.count(k-p.F-disty)==0)continue;
      ans=min(ans,mp[k-p.F-disty]+p.S);
    }
    for(auto itr=mp1.begin();itr!=mp1.end();itr++){
      pair<int,int> p=*itr;
      if(mp.count(p.F+disty)==0)mp[p.F+disty]=p.S+1;
      else mp[p.F+disty]=min(mp[p.F+disty],p.S+1);
    }
  }
  map<ll,int> nmap;
  if(x==cen){
    for(auto itr=mp.begin();itr!=mp.end();itr++){
      pair<ll,int> p=*itr;
      if(nmap.count(p.F+dist[cen])==0)nmap[p.F+dist[cen]]=p.S;
      else nmap[p.F+dist[cen]]=min(nmap[p.F+dist[cen]],p.S);
    }
    nmap[0]=1;
    return nmap;
  }
  y=cenpar;disty=cenparlen;
  mp1=func(y,cen,s);
  for(auto itr=mp1.begin();itr!=mp1.end();itr++){
    pair<int,int> p=*itr;
    if(mp.count(k-p.F-disty)==0)continue;
    ans=min(ans,mp[k-p.F-disty]+p.S);
  }
  queue<int> q;q.P(x);
  parent[x]=prcen;len[x]=1;dist[x]=0;
  while(!q.empty()){
    int c=q.front();q.pop();
    if(c==cen)continue;
    for(int i=0;i<v[c].size();i++){
      y=v[c][i].F;
      disty=v[c][i].S;
      if(parent[c]==y || s.count(y))continue;
      parent[y]=c;
      len[y]=len[c]+1;
      dist[y]=dist[c]+disty;
      if(nmap.count(dist[y])==0)nmap[dist[y]]=len[y];
      else nmap[dist[y]]=min(nmap[dist[y]],len[y]);
      q.P(y);
    }
  }
  for(auto itr=mp.begin();itr!=mp.end();itr++){
    pair<ll,int> p=*itr;
    if(nmap.count(p.F+dist[cen])==0)nmap[p.F+dist[cen]]=p.S+len[cen]-1;
    else nmap[p.F+dist[cen]]=min(nmap[p.F+dist[cen]],p.S+len[cen]-1);
  }
  nmap[0]=1;
  return nmap;
}
int best_path(int N, int K, int h[][2], int l[]){
  n=(ll)N;k=(ll)K;
  for(int i=0;i<n-1;i++){
    v[h[i][0]].PB(MP(h[i][1],(ll)l[i]));
    v[h[i][1]].PB(MP(h[i][0],(ll)l[i]));
  }
  set<int> s;
  func(0,0,s);
  if(ans>n)return -1;
  return ans-1;
}

Compilation message

race.cpp: In function 'int dfs(int, std::set<int>)':
race.cpp:22:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |   for(int i=0;i<v[x].size();i++){
      |               ~^~~~~~~~~~~~
race.cpp: In function 'int find_centroid(int, int, std::set<int>, int)':
race.cpp:34:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |   for(int i=0;i<v[x].size();i++){
      |               ~^~~~~~~~~~~~
race.cpp: In function 'std::map<long long int, int> func(int, int, std::set<int>)':
race.cpp:52:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |   for(int i=0;i<v[cen].size();i++){
      |               ~^~~~~~~~~~~~~~
race.cpp:95:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |     for(int i=0;i<v[c].size();i++){
      |                 ~^~~~~~~~~~~~
race.cpp:87:22: warning: 'cenparlen' may be used uninitialized in this function [-Wmaybe-uninitialized]
   87 |     if(mp.count(k-p.F-disty)==0)continue;
      |                 ~~~~~^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5012 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5004 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 4 ms 5000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5012 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5004 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 4 ms 5000 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 4 ms 4948 KB Output is correct
21 Correct 5 ms 5076 KB Output is correct
22 Correct 8 ms 5204 KB Output is correct
23 Correct 7 ms 5140 KB Output is correct
24 Correct 7 ms 5204 KB Output is correct
25 Correct 6 ms 5204 KB Output is correct
26 Correct 6 ms 5144 KB Output is correct
27 Correct 6 ms 5076 KB Output is correct
28 Correct 6 ms 5204 KB Output is correct
29 Correct 6 ms 5204 KB Output is correct
30 Correct 6 ms 5204 KB Output is correct
31 Correct 6 ms 5204 KB Output is correct
32 Correct 6 ms 5140 KB Output is correct
33 Correct 6 ms 5204 KB Output is correct
34 Correct 9 ms 5276 KB Output is correct
35 Correct 8 ms 5268 KB Output is correct
36 Correct 6 ms 5264 KB Output is correct
37 Correct 7 ms 5264 KB Output is correct
38 Correct 7 ms 5236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5012 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5004 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 4 ms 5000 KB Output is correct
19 Correct 457 ms 14044 KB Output is correct
20 Correct 441 ms 14744 KB Output is correct
21 Correct 454 ms 14732 KB Output is correct
22 Correct 396 ms 14760 KB Output is correct
23 Correct 692 ms 15620 KB Output is correct
24 Correct 223 ms 14488 KB Output is correct
25 Correct 892 ms 25864 KB Output is correct
26 Correct 693 ms 32212 KB Output is correct
27 Correct 597 ms 25312 KB Output is correct
28 Correct 2775 ms 80780 KB Output is correct
29 Correct 2770 ms 78524 KB Output is correct
30 Correct 760 ms 25304 KB Output is correct
31 Correct 593 ms 25308 KB Output is correct
32 Correct 675 ms 25548 KB Output is correct
33 Correct 1667 ms 25344 KB Output is correct
34 Correct 1988 ms 49720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5012 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5004 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 4 ms 5000 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 4 ms 4948 KB Output is correct
21 Correct 5 ms 5076 KB Output is correct
22 Correct 8 ms 5204 KB Output is correct
23 Correct 7 ms 5140 KB Output is correct
24 Correct 7 ms 5204 KB Output is correct
25 Correct 6 ms 5204 KB Output is correct
26 Correct 6 ms 5144 KB Output is correct
27 Correct 6 ms 5076 KB Output is correct
28 Correct 6 ms 5204 KB Output is correct
29 Correct 6 ms 5204 KB Output is correct
30 Correct 6 ms 5204 KB Output is correct
31 Correct 6 ms 5204 KB Output is correct
32 Correct 6 ms 5140 KB Output is correct
33 Correct 6 ms 5204 KB Output is correct
34 Correct 9 ms 5276 KB Output is correct
35 Correct 8 ms 5268 KB Output is correct
36 Correct 6 ms 5264 KB Output is correct
37 Correct 7 ms 5264 KB Output is correct
38 Correct 7 ms 5236 KB Output is correct
39 Correct 457 ms 14044 KB Output is correct
40 Correct 441 ms 14744 KB Output is correct
41 Correct 454 ms 14732 KB Output is correct
42 Correct 396 ms 14760 KB Output is correct
43 Correct 692 ms 15620 KB Output is correct
44 Correct 223 ms 14488 KB Output is correct
45 Correct 892 ms 25864 KB Output is correct
46 Correct 693 ms 32212 KB Output is correct
47 Correct 597 ms 25312 KB Output is correct
48 Correct 2775 ms 80780 KB Output is correct
49 Correct 2770 ms 78524 KB Output is correct
50 Correct 760 ms 25304 KB Output is correct
51 Correct 593 ms 25308 KB Output is correct
52 Correct 675 ms 25548 KB Output is correct
53 Correct 1667 ms 25344 KB Output is correct
54 Correct 1988 ms 49720 KB Output is correct
55 Correct 41 ms 6748 KB Output is correct
56 Correct 31 ms 5972 KB Output is correct
57 Correct 436 ms 15160 KB Output is correct
58 Correct 67 ms 13908 KB Output is correct
59 Correct 924 ms 42876 KB Output is correct
60 Correct 2648 ms 79136 KB Output is correct
61 Correct 661 ms 28940 KB Output is correct
62 Correct 556 ms 25380 KB Output is correct
63 Correct 688 ms 25500 KB Output is correct
64 Correct 1575 ms 38796 KB Output is correct
65 Correct 1331 ms 50276 KB Output is correct
66 Correct 2633 ms 73400 KB Output is correct
67 Correct 181 ms 24140 KB Output is correct
68 Correct 831 ms 46300 KB Output is correct
69 Correct 837 ms 46572 KB Output is correct
70 Correct 786 ms 44488 KB Output is correct