#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define ll int
map<pair<ll,ll>,ll>mp;
ll pm[50010];
ll m[50010];
ll n,cnt,a1,a2,a3,a4,a5;
bool used[50010];
vector<ll>g[50010];
ll ctn[50010],pos[50010],pre[50010];
bool was[50010];
ll nom=1;
void dfs(ll v,ll p)
{
if(m[v]==1)
{
pm[v]=1;
}
used[v]=1;
for(int it:g[v])
{
if(used[it]!=1)
{
dfs(it,v);
}
}
if(pm[v]==1)
{
cnt+=mp[{p,v}];
pm[p]=max(pm[p],1);
pm[v]=2;
}
}
void bfs(ll v,ll p)
{
pos[v]=nom;
nom++;
was[v]=1;
pre[pos[v]]=pre[pos[p]]+mp[{v,p}];
for(int it:g[v])
{
if(was[it]==0)
{
bfs(it,v);
}
}
}
int main()
{
ios_base::sync_with_stdio();
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
ll q,u,v,c;
bool BANKAI=1;
ll st=0;
for(int i=1;i<n;i++)
{
//cin>>u>>v>>c;
scanf("%d%d%d",&u,&v,&c);
g[u].push_back(v);
g[v].push_back(u);
ctn[v]++;ctn[u]++;
if(ctn[v]>2 || ctn[u]>2)BANKAI=0;
mp[{u,v}]=c;
mp[{v,u}]=c;
}
cin>>q;
if(BANKAI==0)
{
while(q--)
{
scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
//cin>>a1>>a2>>a3>>a4>>a5;
m[a2]=m[a3]=m[a4]=m[a5]=1;
dfs(a1,-1);
cout<<cnt<<"\n";
m[a2]=m[a3]=m[a4]=m[a5]=0;
cnt=0;
for(int i=0;i<n;i++)pm[i]=0,used[i]=0;
}
}
else
{
for(int i=0;i<n;i++)
{
if(ctn[i]==1)
{
st=i;break;
}
}
bfs(st,0);
while(q--)
{
scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
ll mn=min({pos[a1],pos[a2],pos[a3],pos[a4],pos[a5]});
ll mx=max({pos[a1],pos[a2],pos[a3],pos[a4],pos[a5]});
cout<<pre[mx]-pre[mn]<<"\n";
}
}
}
Compilation message
roadsideadverts.cpp: In function 'int main()':
roadsideadverts.cpp:64:11: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%d%d%d",&u,&v,&c);
| ~~~~~^~~~~~~~~~~~~~~~~~~
roadsideadverts.cpp:77:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
roadsideadverts.cpp:100:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
15184 KB |
Output is correct |
2 |
Correct |
76 ms |
15248 KB |
Output is correct |
3 |
Correct |
61 ms |
15128 KB |
Output is correct |
4 |
Correct |
67 ms |
15204 KB |
Output is correct |
5 |
Correct |
68 ms |
15300 KB |
Output is correct |
6 |
Correct |
66 ms |
15124 KB |
Output is correct |
7 |
Correct |
69 ms |
15044 KB |
Output is correct |
8 |
Correct |
64 ms |
15184 KB |
Output is correct |
9 |
Correct |
75 ms |
15052 KB |
Output is correct |
10 |
Correct |
61 ms |
15184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
10332 KB |
Output is correct |
2 |
Correct |
974 ms |
14692 KB |
Output is correct |
3 |
Correct |
928 ms |
14712 KB |
Output is correct |
4 |
Correct |
995 ms |
14684 KB |
Output is correct |
5 |
Correct |
911 ms |
14676 KB |
Output is correct |
6 |
Correct |
841 ms |
14916 KB |
Output is correct |
7 |
Correct |
956 ms |
14720 KB |
Output is correct |
8 |
Correct |
984 ms |
14928 KB |
Output is correct |
9 |
Correct |
949 ms |
14684 KB |
Output is correct |
10 |
Correct |
985 ms |
15540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
59 ms |
15184 KB |
Output is correct |
3 |
Correct |
76 ms |
15248 KB |
Output is correct |
4 |
Correct |
61 ms |
15128 KB |
Output is correct |
5 |
Correct |
67 ms |
15204 KB |
Output is correct |
6 |
Correct |
68 ms |
15300 KB |
Output is correct |
7 |
Correct |
66 ms |
15124 KB |
Output is correct |
8 |
Correct |
69 ms |
15044 KB |
Output is correct |
9 |
Correct |
64 ms |
15184 KB |
Output is correct |
10 |
Correct |
75 ms |
15052 KB |
Output is correct |
11 |
Correct |
61 ms |
15184 KB |
Output is correct |
12 |
Correct |
161 ms |
10332 KB |
Output is correct |
13 |
Correct |
974 ms |
14692 KB |
Output is correct |
14 |
Correct |
928 ms |
14712 KB |
Output is correct |
15 |
Correct |
995 ms |
14684 KB |
Output is correct |
16 |
Correct |
911 ms |
14676 KB |
Output is correct |
17 |
Correct |
841 ms |
14916 KB |
Output is correct |
18 |
Correct |
956 ms |
14720 KB |
Output is correct |
19 |
Correct |
984 ms |
14928 KB |
Output is correct |
20 |
Correct |
949 ms |
14684 KB |
Output is correct |
21 |
Correct |
985 ms |
15540 KB |
Output is correct |
22 |
Correct |
60 ms |
16132 KB |
Output is correct |
23 |
Execution timed out |
1051 ms |
11464 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |