#include "factories.h"
#include <bits/stdc++.h>
#define MAXN 500007
#define MAXL 20
using namespace std;
vector<int> g[MAXN];
vector<long long> c[MAXN];
int p[MAXN],n,sz,cn,in[MAXN],gl,arr[MAXN],d[MAXN];
long long be[MAXN][MAXL],opt[MAXN];
bool vc[MAXN];
int dfssize(int s,int f)
{
int x=1;
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && !vc[g[s][i]]) x+=dfssize(g[s][i],s);
return x;
}
int dfsc(int s,int f)
{
int x=1;
bool ce=true;
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && !vc[g[s][i]])
{
int a=dfsc(g[s][i],s);
if(a>sz/2) ce=false;
x+=a;
}
if(sz-x>sz/2) ce=false;
if(ce) cn=s;
return x;
}
void dfs(int s,int f,long long dist,int po)
{
if(d[s]<d[po]) return;
be[s][d[s]-d[po]]=dist;
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs(g[s][i],s,dist+c[s][i],po);
}
void centrodecomp(int s,int f,int du)
{
sz=dfssize(s,-1);
dfsc(s,-1);
p[cn]=f;
vc[cn]=true;
d[cn]=du;
int tmp=cn;
for(int i=0;i<g[tmp].size();i++) if(!vc[g[tmp][i]]) centrodecomp(g[tmp][i],tmp,du+1);
}
void Init(int N, int A[], int B[], int D[])
{
n=N;
int root;
for(int i=0;i<n-1;i++) {g[A[i]].push_back(B[i]); g[B[i]].push_back(A[i]); c[A[i]].push_back(D[i]); c[B[i]].push_back(D[i]);}
centrodecomp(0,-1,0);
for(int i=0;i<n;i++) dfs(i,-1,0,i);
fill(opt,opt+MAXN,1000000000000000000LL);
}
long long Query(int S, int X[], int T, int Y[])
{
int s=S,t=T;
long long sk=1000000000000000000LL;
for(int i=0;i<s;i++)
{
int a=X[i],cnt=0;
while(a!=-1) {opt[a]=min(opt[a],be[X[i]][cnt]); a=p[a]; cnt++;}
}
for(int i=0;i<t;i++)
{
int a=Y[i],cnt=0;
while(a!=-1) {sk=min(sk,be[Y[i]][cnt]+opt[a]); a=p[a]; cnt++;}
}
for(int i=0;i<s;i++)
{
int a=X[i];
while(a!=-1) {opt[a]=1000000000000000000LL; a=p[a];}
}
return sk;
}
Compilation message
factories.cpp: In function 'int dfssize(int, int)':
factories.cpp:14:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && !vc[g[s][i]]) x+=dfssize(g[s][i],s);
~^~~~~~~~~~~~
factories.cpp: In function 'int dfsc(int, int)':
factories.cpp:21:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && !vc[g[s][i]])
~^~~~~~~~~~~~
factories.cpp: In function 'void dfs(int, int, long long int, int)':
factories.cpp:35:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs(g[s][i],s,dist+c[s][i],po);
~^~~~~~~~~~~~
factories.cpp: In function 'void centrodecomp(int, int, int)':
factories.cpp:45:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[tmp].size();i++) if(!vc[g[tmp][i]]) centrodecomp(g[tmp][i],tmp,du+1);
~^~~~~~~~~~~~~~
factories.cpp: In function 'void Init(int, int*, int*, int*)':
factories.cpp:50:6: warning: unused variable 'root' [-Wunused-variable]
int root;
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
28152 KB |
Output is correct |
2 |
Correct |
425 ms |
46688 KB |
Output is correct |
3 |
Correct |
439 ms |
56236 KB |
Output is correct |
4 |
Correct |
597 ms |
65764 KB |
Output is correct |
5 |
Correct |
485 ms |
75596 KB |
Output is correct |
6 |
Correct |
397 ms |
84628 KB |
Output is correct |
7 |
Correct |
441 ms |
94312 KB |
Output is correct |
8 |
Correct |
433 ms |
103652 KB |
Output is correct |
9 |
Correct |
455 ms |
113384 KB |
Output is correct |
10 |
Correct |
356 ms |
122628 KB |
Output is correct |
11 |
Correct |
455 ms |
132164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
132164 KB |
Output is correct |
2 |
Correct |
4263 ms |
271952 KB |
Output is correct |
3 |
Execution timed out |
6026 ms |
286968 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4376 ms |
291408 KB |
Output is correct |
2 |
Correct |
4533 ms |
318472 KB |
Output is correct |
3 |
Execution timed out |
6105 ms |
338084 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |