답안 #27085

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
27085 2017-07-09T07:54:01 Z 검수컵(#1129) 2017 지구멸망 (FXCUP2_apocalypse) C++
1 / 1
283 ms 20060 KB
#include<stdio.h>
#include<algorithm>
using namespace std;

typedef long long lld;
const lld mod = 1000000009;

struct edg {
	int s, e; lld v;
	bool operator< (const edg& c) const {
		return v>c.v;
	}
} ba[303030];

int N; lld prob[303030]; // probability that each edge is in MST
int par[303030], vcn[303030]; lld vxs[303030], vxpxs[303030], vxpxsum;
lld ex2[303030], rv2[303030], dap1, dap2;

int root(int ix){ return par[ix]<0 ? ix : par[ix]=root(par[ix]); }

int main(){
	scanf("%d", &N);
	for(int i=0; i<N; i++) scanf("%d%d%lld", &ba[i].s, &ba[i].e, &ba[i].v);
	sort(ba, ba+N);
	ex2[0]=rv2[0]=1;
	for(int i=1; i<=N; i++) ex2[i]=ex2[i-1]*2%mod, rv2[i]=rv2[i-1]*500000005%mod;
	for(int i=0; i<=N; i++) par[i]=-1, vcn[i]=1;
	for(int i=0; i<N; i++){
		int s=root(ba[i].s), e=root(ba[i].e), k=root(0);
		int a=vcn[s], b=vcn[e];
		if(s==k){
			prob[i] = rv2[b];
			dap2 = (dap2 + (ba[i].v * rv2[b] % mod) * (vxs[e] + vxpxs[s])) % mod;
		}
		else if(e==k){
			prob[i] = rv2[a];
			dap2 = (dap2 + (ba[i].v * rv2[a] % mod) * (vxs[s] + vxpxs[e])) % mod;
		}
		else{
			prob[i] = (ex2[a] + ex2[b] - 1) * rv2[a+b] % mod;
			dap2 = (dap2 + (ba[i].v * (ex2[a]-1) % mod) * (rv2[a+b] * vxs[e] % mod)) % mod;
			dap2 = (dap2 + ba[i].v * (rv2[a] * vxpxs[e] % mod)) % mod;
			dap2 = (dap2 + (ba[i].v * (ex2[b]-1) % mod) * (rv2[a+b] * vxs[s] % mod)) % mod;
			dap2 = (dap2 + ba[i].v * (rv2[b] * vxpxs[s] % mod)) % mod;
		}
		dap2 = (dap2 + (vxpxsum - vxpxs[s] - vxpxs[e] + mod*2) * (ba[i].v * prob[i] % mod)) % mod;
		vxpxsum = (vxpxsum + ba[i].v * prob[i]) % mod;

		if(vcn[s] > vcn[e]){
			par[e]=s; vcn[s]+=vcn[e];
			vxs[s] = (vxs[s] + vxs[e] + ba[i].v) % mod;
			vxpxs[s] = (vxpxs[s] + vxpxs[e] + ba[i].v*prob[i]) % mod;
		}
		else{
			par[s]=e; vcn[e]+=vcn[s];
			vxs[e] = (vxs[s] + vxs[e] + ba[i].v) % mod;
			vxpxs[e] = (vxpxs[s] + vxpxs[e] + ba[i].v*prob[i]) % mod;
		}
	}
	dap2 = dap2*2 % mod;

	for(int i=0; i<N; i++){
		dap1 = (dap1 + ba[i].v * prob[i]) % mod;
		dap2 = (dap2 + (ba[i].v * ba[i].v % mod) * prob[i]) % mod;
	}
	printf("%lld\n%lld\n", dap1 * ex2[N] % mod, dap2 * ex2[N] % mod);
	return 0;
}

Compilation message

apocalypse.cpp: In function 'int main()':
apocalypse.cpp:22:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
                 ^
apocalypse.cpp:23:72: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=0; i<N; i++) scanf("%d%d%lld", &ba[i].s, &ba[i].e, &ba[i].v);
                                                                        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 20060 KB Output is correct
2 Correct 0 ms 20060 KB Output is correct
3 Correct 0 ms 20060 KB Output is correct
4 Correct 0 ms 20060 KB Output is correct
5 Correct 0 ms 20060 KB Output is correct
6 Correct 0 ms 20060 KB Output is correct
7 Correct 0 ms 20060 KB Output is correct
8 Correct 0 ms 20060 KB Output is correct
9 Correct 0 ms 20060 KB Output is correct
10 Correct 0 ms 20060 KB Output is correct
11 Correct 0 ms 20060 KB Output is correct
12 Correct 0 ms 20060 KB Output is correct
13 Correct 0 ms 20060 KB Output is correct
14 Correct 3 ms 20060 KB Output is correct
15 Correct 3 ms 20060 KB Output is correct
16 Correct 0 ms 20060 KB Output is correct
17 Correct 0 ms 20060 KB Output is correct
18 Correct 0 ms 20060 KB Output is correct
19 Correct 0 ms 20060 KB Output is correct
20 Correct 0 ms 20060 KB Output is correct
21 Correct 63 ms 20060 KB Output is correct
22 Correct 283 ms 20060 KB Output is correct
23 Correct 246 ms 20060 KB Output is correct
24 Correct 163 ms 20060 KB Output is correct
25 Correct 253 ms 20060 KB Output is correct
26 Correct 243 ms 20060 KB Output is correct
27 Correct 183 ms 20060 KB Output is correct
28 Correct 186 ms 20060 KB Output is correct
29 Correct 196 ms 20060 KB Output is correct
30 Correct 233 ms 20060 KB Output is correct