結果

問題 No.1002 Twotone
ユーザー 👑 kmjpkmjp
提出日時 2020-02-29 00:07:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,221 bytes
コンパイル時間 2,155 ms
コンパイル使用メモリ 183,200 KB
実行使用メモリ 41,156 KB
最終ジャッジ日時 2024-04-21 21:09:25
合計ジャッジ時間 15,346 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,064 KB
testcase_01 AC 4 ms
8,448 KB
testcase_02 AC 3 ms
8,320 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 4 ms
8,320 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 5 ms
8,192 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 4 ms
8,192 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 4 ms
8,320 KB
testcase_27 AC 80 ms
14,184 KB
testcase_28 AC 146 ms
17,256 KB
testcase_29 AC 130 ms
17,260 KB
testcase_30 AC 4 ms
8,320 KB
testcase_31 AC 122 ms
17,256 KB
testcase_32 AC 153 ms
17,384 KB
testcase_33 AC 126 ms
17,388 KB
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

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;
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) {
		ret+=singlea-sing[c1];
		s2[c1]++;
	}
	else {
		ret+=doub[{c1,c2}]+doub[{c2,c1}];
		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);
	
	// first;
	singlea=0;
	sing.clear();
	doub.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(d,d2) doub[d.first]+=d.second;
		s2.clear();
		d2.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