# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
16419 |
2015-08-23T07:17:53 Z |
comet |
Race (IOI11_race) |
C++ |
|
5 ms |
4324 KB |
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#define pb push_back
using namespace std;
struct edge{
int u,c;
};
struct state{
int v,c,len;
};
typedef long long ll;
typedef vector<int> vec;
typedef vector<edge> Evec;
vector<Evec> path;
int N,K;
int a[1000010];
int SubSz[200000],MaxSz[200000];
bool cover[200000],chk[200000];
int dfs(int v,int k){
chk[v]=!k;
SubSz[v]=MaxSz[v]=1;
for(int i=0;i<path[v].size();i++){
int u=path[v][i].u;
if(chk[u]==k&&!cover[u]){
int t=dfs(u,k);
SubSz[v]+=t;
MaxSz[v]=max(MaxSz[v],t+1);
}
}
return SubSz[v];
}
int centroid(int root,int cnt){
//printf("centroid %d %d\n",root,cnt);
dfs(root,0);
int Min=1e9,ret;
for(int i=0;i<N;i++){
if(chk[i]&&!cover[i]){
//printf("%d : %d, %d\n",i,MaxSz[i],SubSz[i]);
if(Min>max(MaxSz[i],cnt-MaxSz[i])){
Min=max(MaxSz[i],cnt-MaxSz[i]);
ret=i;
}
}
}
return ret;
}
int f(int root,int cnt){
if(cnt==1)return 1e9;
root=centroid(root,cnt);
cover[root]=1;
int ret=1e9;
//printf("root : %d %d\n",root,cnt);
dfs(root,1);
//for(int i=0;i<N;i++)printf("%d ",cover[i]);puts("");
for(int i=0;i<path[root].size();i++){
int nxt=path[root][i].u;
int cost=path[root][i].c;
if(cover[nxt])continue;
//printf("%d -> %d\n",root,nxt);
ret=min(ret,f(nxt,SubSz[nxt]));
//printf("%d-->%d : %d\n",root,nxt,ret);
}
for(int i=0;i<path[root].size();i++){
int nxt=path[root][i].u;
int cost=path[root][i].c;
if(cover[nxt])continue;
queue<state> Q;
Q.push(state{nxt,cost,1});
while(!Q.empty()){
state h=Q.front();
Q.pop();
if(h.c>K)continue;
ret=min(ret,a[K-h.c]+h.len);
for(int j=0;j<path[h.v].size();j++){
int u=path[h.v][j].u;
if(!cover[u]&&!chk[u]){
chk[u]=1;
Q.push(state{u,h.c+path[h.v][j].c,h.len+1});
}
}
}
Q.push(state{nxt,cost,1});
while(!Q.empty()){
state h=Q.front();
Q.pop();
if(h.c>K)continue;
if(h.c==K)ret=min(ret,h.len);
a[h.c]=min(a[h.c],h.len);
for(int j=0;j<path[h.v].size();j++){
int u=path[h.v][j].u;
if(!cover[u]&&chk[u]){
chk[u]=0;
Q.push(state{u,h.c+path[h.v][j].c,h.len+1});
}
}
}
}
for(int i=0;i<path[root].size();i++){
int nxt=path[root][i].u;
int cost=path[root][i].c;
if(cover[nxt])continue;
queue<state> Q;
Q.push(state{nxt,cost,1});
while(!Q.empty()){
state h=Q.front();
Q.pop();
if(h.c>K)continue;
for(int j=0;j<path[h.v].size();j++){
int u=path[h.v][j].u;
if(!cover[u]&&!chk[u]){
chk[u]=1;
Q.push(state{u,h.c+path[h.v][j].c,h.len+1});
}
}
}
Q.push(state{nxt,cost,1});
while(!Q.empty()){
state h=Q.front();
Q.pop();
if(h.c>K)continue;
a[h.c]=1e9;
for(int j=0;j<path[h.v].size();j++){
int u=path[h.v][j].u;
if(!cover[u]&&chk[u]){
chk[u]=0;
Q.push(state{u,h.c+path[h.v][j].c,h.len+1});
}
}
}
}
cover[root]=0;
return ret;
}
int best_path(int N_,int K_,int H[][2],int L[]){
N=N_,K=K_;
memset(a,0x3f,sizeof(a));
path.assign(N,Evec());
for(int i=0;i<N-1;i++){
path[H[i][0]].pb(edge{H[i][1],L[i]});
path[H[i][1]].pb(edge{H[i][0],L[i]});
}
int ret=f(0,N);
if(ret>N-1)return -1;
return ret;
}
Compilation message
race.cpp: In function 'int dfs(int, int)':
race.cpp:28:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<path[v].size();i++){
~^~~~~~~~~~~~~~~
race.cpp: In function 'int f(int, int)':
race.cpp:66:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<path[root].size();i++){
~^~~~~~~~~~~~~~~~~~
race.cpp:68:7: warning: unused variable 'cost' [-Wunused-variable]
int cost=path[root][i].c;
^~~~
race.cpp:76:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<path[root].size();i++){
~^~~~~~~~~~~~~~~~~~
race.cpp:89:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<path[h.v].size();j++){
~^~~~~~~~~~~~~~~~~
race.cpp:104:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<path[h.v].size();j++){
~^~~~~~~~~~~~~~~~~
race.cpp:114:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<path[root].size();i++){
~^~~~~~~~~~~~~~~~~~
race.cpp:125:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<path[h.v].size();j++){
~^~~~~~~~~~~~~~~~~
race.cpp:139:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<path[h.v].size();j++){
~^~~~~~~~~~~~~~~~~
race.cpp: In function 'int centroid(int, int)':
race.cpp:52:9: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
return ret;
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4324 KB |
Output is correct |
3 |
Incorrect |
5 ms |
4324 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4324 KB |
Output is correct |
3 |
Incorrect |
5 ms |
4324 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4324 KB |
Output is correct |
3 |
Incorrect |
5 ms |
4324 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4324 KB |
Output is correct |
3 |
Incorrect |
5 ms |
4324 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |