# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
318975 |
2020-11-03T15:28:14 Z |
Dymo |
Factories (JOI14_factories) |
C++14 |
|
4379 ms |
191304 KB |
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define ll long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define endl "\n"
const ll maxn=5e5+54;
const ll mod =1e9+7 ;
const ll base=1e18;
vector<pll> adj[maxn];
int dep[maxn];
ll dep1[maxn];
int anc[maxn][20];
int col[maxn];
vector<ll> adj1[maxn];
int f[maxn];
int l[maxn];
int cnt=0;
void dfs(ll u,ll par)
{
cnt++;
f[u]=cnt;
anc[u][0]=par;
for (int i=1;i<19;i++)
{
anc[u][i]=anc[anc[u][i-1]][i-1];
}
for (auto to:adj[u])
{
if (to.ff==par) continue;
dep[to.ff]=dep[u]+1;
dep1[to.ff]=dep1[u]+to.ss;
dfs(to.ff,u);
}
l[u]=cnt;
}
ll lca(ll x,ll y)
{
if (dep[x]< dep[y]) swap(x,y);
ll kc= dep[x]-dep[y];
for (int i=18;i>=0;i--)
{
if (kc&(1ll<<i))
{
x=anc[x][i];
}
}
if (x==y) return x;
for (int i=18;i>=0;i--)
{
if (anc[x][i]!=anc[y][i])
{
x=anc[x][i];
y=anc[y][i];
}
}
return anc[x][0];
}
ll kc(ll x,ll y)
{
return dep1[x]+dep1[y]-2*(dep1[lca(x,y)]);
}
ll dp1[maxn];
ll dp2[maxn];
ll ans=1e18;
void dfs1(ll u)
{
dp1[u]=dp2[u]=1e15;
if (col[u]==1) dp1[u]=dep1[u];
else if (col[u]==2) dp2[u]=dep1[u];
for (auto to:adj1[u])
{
dfs1(to);
ans=min(ans,dp1[u]+dp2[to]-dep1[u]*2);
ans=min(ans,dp2[u]+dp1[to]-dep1[u]*2);
dp1[u]=min(dp1[u],dp1[to]);
dp2[u]=min(dp2[u],dp2[to]);
}
}
void Init(int N,int A[],int B[],int D[])
{
ll n= N;
for (int i=1;i<=n;i++)
{
adj[i].clear();
}
for (int i=0;i<n-1;i++)
{
A[i]++;
B[i]++;
adj[A[i]].pb(make_pair(B[i],D[i]));
adj[B[i]].pb(make_pair(A[i],D[i]));
}
cnt=0;
dfs(1,0);
for (int i=1;i<=n;i++)
{
adj[i].clear();
}
}
long long Query(int S,int X[],int T,int Y[])
{
ans=1e16;
vector<pll> vt;
for (int i=0;i<S;i++)
{
X[i]++;
col[X[i]]=1;
vt.pb(make_pair(f[X[i]],X[i]));
}
for (int i=0;i<T;i++)
{
Y[i]++;
col[Y[i]]=2;
vt.pb(make_pair(f[Y[i]],Y[i]));
}
sort(vt.begin(),vt.end());
vector<pll> nw;
for (int i=1;i<vt.size();i++)
{
ll h=lca(vt[i].ss,vt[i-1].ss);
nw.pb(make_pair(f[h],h));
}
for (auto p:vt)
{
nw.pb(p);
}
nw.pb(make_pair(f[1],1));
sort(nw.begin(),nw.end());
nw.resize(unique(nw.begin(),nw.end())-nw.begin());
stack<ll> st;
for (auto p:nw)
{
while (st.size()&&l[st.top()]<p.ff) st.pop();
if (st.size())
{
adj1[st.top()].pb(p.ss);
}
st.push(p.ss);
}
dfs1(1);
for (auto p:nw)
{
col[p.ss]=0;
adj1[p.ss].clear();
}
return ans;
}
/*int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen("t.txt", "r"))
{
freopen("test.txt", "r", stdin);
// freopen("team.out", "w", stdout);
}
}*/
Compilation message
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:131: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]
131 | for (int i=1;i<vt.size();i++)
| ~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
24428 KB |
Output is correct |
2 |
Correct |
1077 ms |
35820 KB |
Output is correct |
3 |
Correct |
1024 ms |
35748 KB |
Output is correct |
4 |
Correct |
1056 ms |
36104 KB |
Output is correct |
5 |
Correct |
917 ms |
36520 KB |
Output is correct |
6 |
Correct |
860 ms |
36016 KB |
Output is correct |
7 |
Correct |
1030 ms |
36084 KB |
Output is correct |
8 |
Correct |
1006 ms |
36704 KB |
Output is correct |
9 |
Correct |
928 ms |
36828 KB |
Output is correct |
10 |
Correct |
845 ms |
36596 KB |
Output is correct |
11 |
Correct |
1018 ms |
36580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
24044 KB |
Output is correct |
2 |
Correct |
1958 ms |
121304 KB |
Output is correct |
3 |
Correct |
2760 ms |
142412 KB |
Output is correct |
4 |
Correct |
1414 ms |
137152 KB |
Output is correct |
5 |
Correct |
2782 ms |
178684 KB |
Output is correct |
6 |
Correct |
2960 ms |
144668 KB |
Output is correct |
7 |
Correct |
2586 ms |
66464 KB |
Output is correct |
8 |
Correct |
1305 ms |
65872 KB |
Output is correct |
9 |
Correct |
2519 ms |
72992 KB |
Output is correct |
10 |
Correct |
2731 ms |
67548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
24428 KB |
Output is correct |
2 |
Correct |
1077 ms |
35820 KB |
Output is correct |
3 |
Correct |
1024 ms |
35748 KB |
Output is correct |
4 |
Correct |
1056 ms |
36104 KB |
Output is correct |
5 |
Correct |
917 ms |
36520 KB |
Output is correct |
6 |
Correct |
860 ms |
36016 KB |
Output is correct |
7 |
Correct |
1030 ms |
36084 KB |
Output is correct |
8 |
Correct |
1006 ms |
36704 KB |
Output is correct |
9 |
Correct |
928 ms |
36828 KB |
Output is correct |
10 |
Correct |
845 ms |
36596 KB |
Output is correct |
11 |
Correct |
1018 ms |
36580 KB |
Output is correct |
12 |
Correct |
19 ms |
24044 KB |
Output is correct |
13 |
Correct |
1958 ms |
121304 KB |
Output is correct |
14 |
Correct |
2760 ms |
142412 KB |
Output is correct |
15 |
Correct |
1414 ms |
137152 KB |
Output is correct |
16 |
Correct |
2782 ms |
178684 KB |
Output is correct |
17 |
Correct |
2960 ms |
144668 KB |
Output is correct |
18 |
Correct |
2586 ms |
66464 KB |
Output is correct |
19 |
Correct |
1305 ms |
65872 KB |
Output is correct |
20 |
Correct |
2519 ms |
72992 KB |
Output is correct |
21 |
Correct |
2731 ms |
67548 KB |
Output is correct |
22 |
Correct |
3398 ms |
160736 KB |
Output is correct |
23 |
Correct |
3223 ms |
160060 KB |
Output is correct |
24 |
Correct |
3854 ms |
166164 KB |
Output is correct |
25 |
Correct |
3923 ms |
167596 KB |
Output is correct |
26 |
Correct |
4303 ms |
152964 KB |
Output is correct |
27 |
Correct |
3737 ms |
191304 KB |
Output is correct |
28 |
Correct |
2161 ms |
156528 KB |
Output is correct |
29 |
Correct |
4354 ms |
151644 KB |
Output is correct |
30 |
Correct |
4364 ms |
151356 KB |
Output is correct |
31 |
Correct |
4379 ms |
151920 KB |
Output is correct |
32 |
Correct |
2021 ms |
80548 KB |
Output is correct |
33 |
Correct |
1359 ms |
74888 KB |
Output is correct |
34 |
Correct |
2114 ms |
65140 KB |
Output is correct |
35 |
Correct |
2081 ms |
64912 KB |
Output is correct |
36 |
Correct |
2422 ms |
65728 KB |
Output is correct |
37 |
Correct |
2415 ms |
65568 KB |
Output is correct |