結果

問題 No.1002 Twotone
ユーザー 👑 kmjpkmjp
提出日時 2020-02-29 00:46:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,287 ms / 5,000 ms
コード長 2,497 bytes
コンパイル時間 2,482 ms
コンパイル使用メモリ 182,288 KB
実行使用メモリ 52,736 KB
最終ジャッジ日時 2024-04-21 21:15:30
合計ジャッジ時間 18,403 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
8,064 KB
testcase_01 AC 5 ms
8,192 KB
testcase_02 AC 6 ms
8,192 KB
testcase_03 AC 430 ms
15,360 KB
testcase_04 AC 531 ms
17,280 KB
testcase_05 AC 540 ms
17,152 KB
testcase_06 AC 6 ms
8,192 KB
testcase_07 AC 210 ms
13,824 KB
testcase_08 AC 382 ms
17,280 KB
testcase_09 AC 382 ms
17,280 KB
testcase_10 AC 5 ms
8,064 KB
testcase_11 AC 506 ms
16,128 KB
testcase_12 AC 722 ms
18,560 KB
testcase_13 AC 734 ms
18,560 KB
testcase_14 AC 6 ms
8,320 KB
testcase_15 AC 307 ms
13,824 KB
testcase_16 AC 724 ms
17,792 KB
testcase_17 AC 750 ms
18,048 KB
testcase_18 AC 6 ms
8,320 KB
testcase_19 AC 693 ms
22,656 KB
testcase_20 AC 828 ms
31,488 KB
testcase_21 AC 830 ms
30,720 KB
testcase_22 AC 6 ms
8,064 KB
testcase_23 AC 558 ms
19,968 KB
testcase_24 AC 1,139 ms
28,288 KB
testcase_25 AC 1,121 ms
27,136 KB
testcase_26 AC 6 ms
8,320 KB
testcase_27 AC 86 ms
14,440 KB
testcase_28 AC 161 ms
17,384 KB
testcase_29 AC 140 ms
17,512 KB
testcase_30 AC 5 ms
8,192 KB
testcase_31 AC 132 ms
17,260 KB
testcase_32 AC 171 ms
17,384 KB
testcase_33 AC 137 ms
17,384 KB
testcase_34 AC 2,287 ms
52,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,K;
vector<pair<int,int>> E[202020];

int vis[201010];
int NV[201010];

int singlea;
map<int,int> sing,s2,p,p2;
map<pair<int,int>,int> doub,d2;
ll ret=0;

int dfs(int cur,int pre) {
	NV[cur]=1;
	FORR(e,E[cur]) if(e.first!=pre && vis[e.first]==0) NV[cur]+=dfs(e.first,cur);
	return NV[cur];
}

int dfs2(int cur,int pre,int TNV) {
	int tot=1;
	int ok=1;
	FORR(e,E[cur]) if(e.first!=pre && vis[e.first]==0) {
		int c = dfs2(e.first,cur,TNV);
		if(c!=-1) return c;
		tot += NV[e.first];
		if(NV[e.first]*2>TNV) ok=0;
	}
	if((TNV-tot)*2>TNV) ok=0;
	if(ok) return cur;
	return -1;
}

void dfs3(int cur,int pre,int c1,int c2) {
	if(c2==-1) {
		//cout<<cur<<" "<<pre<<" "<<c1<<" "<<c2<<" "<<(singlea-sing[c1])<<endl;
		ret+=singlea-sing[c1];
		ret+=p[c1];
		s2[c1]++;
	}
	else {
		//cout<<cur<<" "<<pre<<" "<<c1<<" "<<c2<<" "<<(doub[{c1,c2}]+doub[{c2,c1}])<<endl;
		ret+=doub[{c1,c2}]+doub[{c2,c1}];
		ret+=sing[c1]+sing[c2];
		p2[c1]++;
		p2[c2]++;
		ret++;
		d2[{c1,c2}]++;
	}
	FORR(e,E[cur]) if(e.first!=pre && vis[e.first]==0) {
		if(c1==e.second || c2==e.second) dfs3(e.first,cur,c1,c2);
		else if(c2==-1) dfs3(e.first,cur,c1,e.second);
	}
}


void split(int cur,int id) {
	int TNV = dfs(cur,-1);
	if(TNV==0) return;
	int center=dfs2(cur,-1,TNV);
	
	singlea=0;
	sing.clear();
	doub.clear();
	p.clear();
	FORR(e,E[center]) if(vis[e.first]==0) {
		dfs3(e.first,center,e.second,-1);
		FORR(s,s2) singlea+=s.second, sing[s.first]+=s.second;
		FORR(a,p2) p[a.first]+=a.second;
		FORR(d,d2) doub[d.first]+=d.second;
		s2.clear();
		d2.clear();
		p2.clear();
	}
	vis[center]=1;
	FORR(e,E[center]) if(vis[e.first]==0) split(e.first,id+1);
	
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	FOR(i,N-1) {
		cin>>x>>y>>r;
		E[x-1].push_back({y-1,r});
		E[y-1].push_back({x-1,r});
	}
	split(0,0);
	cout<<ret<<endl;
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0