QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#103155 | #5313. Please Save Pigeland | zhuidao | WA | 6ms | 42048kb | C++20 | 2.6kb | 2023-05-04 16:53:08 | 2023-05-04 16:53:10 |
Judging History
answer
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>pi;
#define int ll
#define X first
#define Y second
#define fer(i,a,n) for(int i=a;i<=n;i++)
#define ref(i,n,a) for(int i=n;i>=a;i--)
#define endl '\n'
#define mem(a,x) memset(a,x,sizeof a)
#define ac IO;int t;cin>>t;while(t--)solve()
#define AC signed main(){IO;solve();}
#define NO {cout<<"No"<<endl;return;}
#define YES {cout<<"Yes"<<endl;return;}
#define re(a) {cout<<a<<endl;return;}
#define all(v) v.begin(),v.end()
//ofstream fout("out.txt", ios::out);
//ifstream fin("in.txt", ios::in);
//#define cout fout
//#define cin fin
//--------------------瑞神神中神--------------------
//--------------------刘魔魔中魔--------------------
const int N=5e5+10;
struct GCD
{
int f,g;
int calc(){return gcd(f,g);}
GCD(int _f=0,int _g=0):f(_f),g(_g){}
void add(int w)
{
f+=w;
}
GCD operator+(const GCD&a)const
{
if(f==0)return a;
if(a.f==0)return *this;
return{f,gcd(g,gcd(abs(a.f-f),a.g))};
}
}g[N];
int st[N],dis[N],siz[N],ndis,son[N];
int n,k;
vector<pi>e[N];
int ans=1e18;
vector<GCD>vec,pre[N];
GCD ngcd;
void dfs1(int u,int fa)
{
siz[u]=st[u];
for(auto[v,w]:e[u])
{
if(v==fa)continue;
son[u]++;
dfs1(v,u);
siz[u]+=siz[v];dis[u]+=dis[v];
dis[u]+=siz[v]*w;
GCD tmp=g[v];
if(siz[v])tmp.add(w);
g[u]=g[u]+tmp;
if(st[v])g[u]=g[u]+GCD(w,0);
pre[u].push_back(g[u]);
}
}
void dfs2(int u,int fa)
{
ans=min(ans,ndis/(ngcd+g[u]).calc());
GCD suf={0,0};
int i=son[u]-1;
vec.push_back(ngcd);
for(auto it=e[u].rbegin();it!=e[u].rend();it++)
{
auto[v,w]=*it;
if(v==fa)continue;
ndis=ndis+(k-siz[v])*w-siz[v]*w;
ngcd=vec.back();
ngcd=ngcd+suf;
if(i>0)ngcd=ngcd+pre[u][i-1];
if(k-siz[v])ngcd.add(w);
if(st[u])ngcd=ngcd+GCD(w,0);
i--;
suf=suf+g[v];
dfs2(v,u);
}
vec.pop_back();
}
void solve()
{
cin>>n>>k;
fer(i,1,k)
{
int c;
cin>>c;
st[c]=1;
}
for(int i=1;i<n;i++)
{
int a,b,c;
cin>>a>>b>>c;
e[a].emplace_back(b,c);
e[b].emplace_back(a,c);
}
if(k==1)
{
cout<<0<<endl;
return;
}
dfs1(1,0);
ndis=dis[1];ngcd;
dfs2(1,0);
cout<<ans*2<<endl;
}
AC
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 6ms
memory: 42048kb
input:
5 3 3 4 5 1 2 2 2 3 4 2 5 4 3 4 6
output:
8
result:
ok 1 number(s): "8"
Test #2:
score: -100
Wrong Answer
time: 3ms
memory: 40032kb
input:
10 3 1 7 10 7 6 3 1 8 3 3 6 3 8 6 2 4 1 1 10 6 4 2 8 3 9 10 3 5 10 3
output:
14
result:
wrong answer 1st numbers differ - expected: '24', found: '14'