QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#103157#5313. Please Save PigelandzhuidaoWA 1003ms63488kbC++203.2kb2023-05-04 16:54:332023-05-04 16:54:35

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-04 16:54:35]
  • 评测
  • 测评结果:WA
  • 用时:1003ms
  • 内存:63488kb
  • [2023-05-04 16:54:33]
  • 提交

answer

#include <bits/stdc++.h>
#define mk(a,b) make_pair(a,b)
#define int long long
using namespace std;
const int N=500005;
vector<pair<int,int> > e[N];
int t[N],s[N],S[N],dis[N];
int ans;
void dfs1(int u,int fa){
	if(t[u])S[u]++;
	for(auto K:e[u]){
		int v=K.first;
		int d=K.second;
		if(v!=fa){
			dfs1(v,u);
			S[u]+=S[v];
			s[u]+=s[v]+S[v]*d;
		}
	}
}
void dfs2(int u,int fa,int D,int si){
	if(t[u])si++;
	vector<int> p,P,n,N;
	for(int i=0;i<e[u].size();i++){
		int v=e[u][i].first;
		int d=e[u][i].second;
		int A=0,AI=0;
		if(v!=fa){
			A=s[v]+S[v]*d;
			AI=S[v];
		}
		p.emplace_back(A);
		n.emplace_back(A);	
		P.emplace_back(AI);
		N.emplace_back(AI);
		if(i)p[i]+=p[i-1];
		if(i)P[i]+=P[i-1];
	}
	for(int i=e[u].size()-2;i>=0;i--){
		n[i]+=n[i+1];
		N[i]+=N[i+1];
	}
	for(int i=0;i<e[u].size();i++){
		int v=e[u][i].first;
		int d=e[u][i].second;
		if(v==fa)continue;
		int SD=D;
		int SI=si;
		if(i>0){
			SD+=p[i-1];
			SI+=P[i-1];
		}
		if(i<e[u].size()-1){
			SD+=n[i+1];
			SI+=N[i+1];
		}
		dis[v]=s[v]+SD+SI*d;
		ans=min(ans,dis[v]);
		dfs2(v,u,SD+SI*d,SI);
	}
}
pair<int,int> g[N];
pair<int,int> merge(pair<int,int> a,pair<int,int> b){
	if(a.first==-1)return b;
	if(b.first==-1)return a;
	if(a.first<b.first)
		return mk(a.first,__gcd(__gcd(a.second,b.second),b.first-a.first) );
	else
		return mk(b.first,__gcd(__gcd(a.second,b.second),a.first-b.first) );
}
void dp1(int u,int fa){
	vector<pair<int,int> > G;
	for(auto K:e[u]){
		int v=K.first;
		int d=K.second;
		if(v!=fa){
			dp1(v,u);
			if(g[v].first==-1)continue;
			pair<int,int> gq=g[v];
			gq.first+=d;
			G.emplace_back(gq);
		}
	}
	if(G.size()>0){
		for(int i=1;i<G.size();i++)
			G[0]=merge(G[0],G[i]);
		if(t[u]){
			G[0].second=__gcd(G[0].first,G[0].second);
			G[0].first=0;
		}
		g[u]=G[0];
	}else{
		if(t[u]){
			g[u].first=0;
			g[u].second=0;
		}else{
			g[u].first=-1;
			g[u].second=-1;
		}
	}
}
void dp2(int u,int fa,pair<int,int> GcD){
	pair<int,int> Gcd=merge(GcD,g[u]);
	if(__gcd(Gcd.first,Gcd.second)>0)
		ans=min(ans,dis[u]/__gcd(Gcd.first,Gcd.second));
//	cout<<u<<' '<<GcD.first<<' '<<GcD.second<<' '<<Gcd.first<<' '<<Gcd.second<<' '<<dis[u]<<endl;
	if(t[u])GcD=merge(GcD,mk(0,0));
	vector<pair<int,int> > P,N;
	for(int i=0;i<e[u].size();i++){
		int v=e[u][i].first;
		int d=e[u][i].second;
		pair<int,int> A=mk(-1,-1);
		if(v!=fa){
			A=g[v];
			A.first+=d;
		}
		P.emplace_back(A);
		N.emplace_back(A);
	}
	for(int i=1;i<P.size();i++)
		P[i]=merge(P[i],P[i-1]);
	for(int i=N.size()-2;i>=0;i--)
		N[i]=merge(N[i+1],N[i]);
	int cnt=0;
	for(int i=0;i<e[u].size();i++){
		int v=e[u][i].first;
		int d=e[u][i].second;
		if(v==fa)continue;
		pair<int,int> GG=GcD;
		if(i>0)GG=merge(GG,P[i-1]);
		if(i<N.size()-1)GG=merge(GG,N[i+1]);
		if(GG.first!=-1)GG.first+=d;
		dp2(v,u,GG);
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int n,k;
	cin>>n>>k;
	for(int i=1;i<=k;i++){
		int x;cin>>x;
		t[x]=1;
	}
	for(int i=1;i<n;i++){
		int u,v,w;
		cin>>u>>v>>w;
		e[u].emplace_back(mk(v,w));
		e[v].emplace_back(mk(u,w));
	}
	dfs1(1,0);
	dis[1]=s[1];
	ans=dis[1];
	dfs2(1,0,0,0);
	if(ans)dp1(1,0);
	if(ans)dp2(1,0,mk(-1,-1));
	cout<<ans*2;
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 23304kb

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: 0
Accepted
time: 2ms
memory: 21880kb

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:

24

result:

ok 1 number(s): "24"

Test #3:

score: 0
Accepted
time: 6ms
memory: 22772kb

input:

1 1
1

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 79ms
memory: 27304kb

input:

100000 1
79187
72704 72659 15
32741 43496 10
21580 97447 17
55758 36700 21
32116 3643 14
60460 58764 12
75894 50624 7
58295 49393 22
43733 17210 1
58093 68769 15
1086 58916 17
25632 37710 11
49555 92976 8
32547 27060 18
84896 12811 1
3196 1242 16
18870 78236 14
2414 7945 12
48745 15399 1
17648 83791...

output:

0

result:

ok 1 number(s): "0"

Test #5:

score: 0
Accepted
time: 7ms
memory: 20016kb

input:

100 10
3 27 33 45 48 72 76 91 92 100
66 98 4
70 17 2
28 59 4
26 25 3
77 92 1
40 61 2
11 27 2
85 35 1
57 26 1
68 99 4
50 84 1
20 82 3
31 39 1
71 7 4
54 55 4
60 26 4
56 61 2
15 66 3
95 53 2
8 60 4
21 82 1
18 81 2
29 73 3
94 4 1
10 4 4
86 43 1
62 41 1
45 57 1
25 66 3
69 89 2
14 53 3
27 92 1
42 98 4
13 ...

output:

200

result:

ok 1 number(s): "200"

Test #6:

score: 0
Accepted
time: 0ms
memory: 21332kb

input:

100 90
75 17 78 84 52 69 54 24 74 35 58 51 72 7 21 70 93 15 60 87 13 40 23 92 99 53 27 22 91 46 56 86 61 19 44 98 50 28 14 12 55 64 30 80 95 38 18 43 31 89 20 16 8 65 63 79 59 34 97 25 2 11 67 71 29 9 37 76 77 26 39 68 32 62 90 10 85 49 42 45 96 83 94 3 6 100 81 57 88 47
67 65 1
43 78 4
98 71 3
71 2...

output:

1798

result:

ok 1 number(s): "1798"

Test #7:

score: 0
Accepted
time: 1ms
memory: 20080kb

input:

1000 10
111 240 479 530 572 583 644 652 753 869
121 923 2
886 685 4
446 284 4
352 250 1
540 485 2
72 154 4
522 693 3
664 917 4
792 941 3
132 832 4
709 186 3
509 114 2
824 978 2
216 265 2
138 570 1
498 959 4
434 222 1
803 693 1
253 677 4
172 463 3
383 978 2
718 959 3
369 421 4
568 454 4
256 938 1
6 1...

output:

226

result:

ok 1 number(s): "226"

Test #8:

score: 0
Accepted
time: 1ms
memory: 22856kb

input:

1000 957
233 514 228 739 827 85 840 175 766 807 19 276 549 611 145 511 895 121 116 525 280 431 810 629 990 509 542 324 241 801 849 506 178 176 49 528 221 742 444 513 111 505 442 794 107 392 291 674 298 803 198 927 738 590 706 804 860 512 421 618 697 516 335 420 418 288 544 694 330 776 104 510 621 47...

output:

32602

result:

ok 1 number(s): "32602"

Test #9:

score: -100
Wrong Answer
time: 1003ms
memory: 63488kb

input:

500000 4
182462 188845 259396 281751
456733 79213 9204078
395954 45205 3919968
454058 310013 734433
433648 435834 3887333
448797 138275 9946222
385528 63721 3037094
44276 184047 1799127
169565 81666 3752583
459111 229807 5534913
374868 374333 8627923
476055 408523 2692999
445258 424229 3038119
92885...

output:

486956092

result:

wrong answer 1st numbers differ - expected: '193870600', found: '486956092'