提出 #485282


ソースコード 拡げる

#include<cstdio>
#include<unordered_map>
#include<vector>
#define N 100100
using namespace std;
long long ans=0;
unordered_map<int,long long> neg[N],pos[N];
vector<int> g[N];
int f[N],add_pos[N],add_neg[N];
int find(int x){
	if(f[x]==x) return x;
	return f[x]=find(f[x]);
}
char s[N];
bool vis[N];
inline long long get_val(unordered_map<int,long long> &m,int v){
	return m.count(v)?m[v]:0;
}
void DFS(int u){
	vis[u]=true;
	int mc,mv=0,tmp;
	bool leaf=true;
	vector<int> ch;
	for(int i=0;i<g[u].size();i++){
		int v=g[u][i];
		if(vis[v]) continue;
		DFS(v);
		ch.push_back(v);
		if((tmp=pos[find(v)].size()+neg[find(v)].size())>mv){
			mc=v;
			mv=tmp;
		}
		leaf=false;
	}
	if(leaf){
		if(s[u]=='('){
			pos[u][1]++;
		}
		else{
			neg[u][1]++;
		}
		f[u]=u;
		return;
	}
	f[u]=find(mc);
	int p=f[u];
	for(int i=0;i<ch.size();i++){
		int c=ch[i];
		if(c==mc) continue;
		c=find(c);
		for(auto it=pos[c].begin();it!=pos[c].end();++it){
			int tmp=it->first+add_pos[c];
			if(s[u]=='(') tmp++;
			else tmp--;
			if(tmp<0) continue;
			ans+=it->second*get_val(neg[p],tmp-add_neg[p]);
		}
		for(auto it=neg[c].begin();it!=neg[c].end();++it){
			int tmp=it->first+add_neg[c];
			if(s[u]=='(') tmp--;
			else tmp++;
			if(tmp<0) continue;
			ans+=it->second*get_val(pos[p],tmp-add_pos[p]);
		}
		for(auto it=pos[c].begin();it!=pos[c].end();++it){
			pos[p][it->first+add_pos[c]-add_pos[p]]+=it->second;
		}
		pos[c].clear();
		for(auto it=neg[c].begin();it!=neg[c].end();++it){
			neg[p][it->first+add_neg[c]-add_neg[p]]+=it->second;
		}
		neg[c].clear();
	}
	pos[p][0-add_pos[p]]++;
	neg[p][0-add_neg[p]]++;
	if(s[u]=='(') add_pos[p]++,add_neg[p]--;
	else add_pos[p]--,add_neg[p]++;
	auto it=pos[p].find(-1-add_pos[p]);
	if(it!=pos[p].end()) pos[p].erase(it);
	it=neg[p].find(-1-add_neg[p]);
	if(it!=neg[p].end()) neg[p].erase(it);
	ans+=get_val(pos[p],-add_pos[p]);
	ans+=get_val(neg[p],-add_neg[p]);
}
int main(){
	int n,x,y;
	scanf("%d",&n);
	scanf("%s",s+1);
	for(int i=1;i<n;i++){
		scanf("%d%d",&x,&y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	DFS(1);
	printf("%lld\n",ans);
	return 0;
}

提出情報

提出日時
問題 A - Balanced Paths
ユーザ NCTU_Thor
言語 C++11 (GCC 4.9.2)
得点 100
コード長 2190 Byte
結果 AC
実行時間 258 ms
メモリ 55200 KiB

コンパイルエラー

./Main.cpp: In function ‘int main()’:
./Main.cpp:87:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
                ^
./Main.cpp:88:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s",s+1);
                 ^
./Main.cpp:90:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&x,&y);
                      ^

ジャッジ結果

セット名 All
得点 / 配点 100 / 100
結果
AC × 69
セット名 テストケース
All 00_sample_00, 00_sample_01, 00_sample_02, 10_rand_00, 10_rand_01, 10_rand_02, 10_rand_03, 10_rand_04, 10_rand_05, 10_rand_06, 10_rand_07, 10_rand_08, 10_rand_09, 10_rand_10, 11_max_rand_00, 11_max_rand_01, 11_max_rand_02, 11_max_rand_03, 11_max_rand_04, 20_path_00, 20_path_01, 20_path_02, 20_path_03, 20_path_04, 20_path_05, 20_path_06, 20_path_07, 20_path_08, 20_path_09, 20_path_10, 21_max_path_00, 21_max_path_01, 21_max_path_02, 21_max_path_03, 21_max_path_04, 30_star_00, 30_star_01, 30_star_02, 30_star_03, 30_star_04, 30_star_05, 30_star_06, 30_star_07, 30_star_08, 30_star_09, 30_star_10, 31_max_star_00, 31_max_star_01, 31_max_star_02, 31_max_star_03, 31_max_star_04, 40_bintree_00, 40_bintree_01, 40_bintree_02, 40_bintree_03, 40_bintree_04, 40_bintree_05, 40_bintree_06, 40_bintree_07, 40_bintree_08, 40_bintree_09, 40_bintree_10, 41_max_bintree_00, 41_max_bintree_01, 41_max_bintree_02, 41_max_bintree_03, 41_max_bintree_04, 90_challenge_00, 91_max_ans_00
ケース名 結果 実行時間 メモリ
00_sample_00 AC 106 ms 32800 KiB
00_sample_01 AC 104 ms 32804 KiB
00_sample_02 AC 103 ms 32808 KiB
10_rand_00 AC 148 ms 34592 KiB
10_rand_01 AC 172 ms 35108 KiB
10_rand_02 AC 196 ms 35492 KiB
10_rand_03 AC 250 ms 37356 KiB
10_rand_04 AC 142 ms 34080 KiB
10_rand_05 AC 215 ms 36012 KiB
10_rand_06 AC 135 ms 33828 KiB
10_rand_07 AC 163 ms 34980 KiB
10_rand_08 AC 149 ms 34344 KiB
10_rand_09 AC 234 ms 37140 KiB
10_rand_10 AC 186 ms 35612 KiB
11_max_rand_00 AC 242 ms 37412 KiB
11_max_rand_01 AC 250 ms 37416 KiB
11_max_rand_02 AC 257 ms 37408 KiB
11_max_rand_03 AC 252 ms 37416 KiB
11_max_rand_04 AC 252 ms 37412 KiB
20_path_00 AC 232 ms 44980 KiB
20_path_01 AC 234 ms 47900 KiB
20_path_02 AC 172 ms 42776 KiB
20_path_03 AC 186 ms 43936 KiB
20_path_04 AC 142 ms 39328 KiB
20_path_05 AC 245 ms 48280 KiB
20_path_06 AC 197 ms 44708 KiB
20_path_07 AC 242 ms 47648 KiB
20_path_08 AC 137 ms 37544 KiB
20_path_09 AC 105 ms 32800 KiB
20_path_10 AC 178 ms 43044 KiB
21_max_path_00 AC 244 ms 46368 KiB
21_max_path_01 AC 252 ms 50208 KiB
21_max_path_02 AC 254 ms 54300 KiB
21_max_path_03 AC 246 ms 47388 KiB
21_max_path_04 AC 246 ms 52244 KiB
30_star_00 AC 224 ms 39960 KiB
30_star_01 AC 187 ms 38172 KiB
30_star_02 AC 195 ms 38168 KiB
30_star_03 AC 177 ms 37148 KiB
30_star_04 AC 168 ms 36508 KiB
30_star_05 AC 173 ms 36636 KiB
30_star_06 AC 124 ms 34084 KiB
30_star_07 AC 144 ms 35492 KiB
30_star_08 AC 116 ms 33700 KiB
30_star_09 AC 215 ms 39148 KiB
30_star_10 AC 148 ms 35872 KiB
31_max_star_00 AC 241 ms 40472 KiB
31_max_star_01 AC 246 ms 40404 KiB
31_max_star_02 AC 244 ms 40472 KiB
31_max_star_03 AC 241 ms 40476 KiB
31_max_star_04 AC 243 ms 40476 KiB
40_bintree_00 AC 180 ms 35360 KiB
40_bintree_01 AC 135 ms 33572 KiB
40_bintree_02 AC 154 ms 34208 KiB
40_bintree_03 AC 209 ms 35880 KiB
40_bintree_04 AC 222 ms 36076 KiB
40_bintree_05 AC 163 ms 34340 KiB
40_bintree_06 AC 230 ms 36772 KiB
40_bintree_07 AC 258 ms 37216 KiB
40_bintree_08 AC 151 ms 34212 KiB
40_bintree_09 AC 184 ms 35368 KiB
40_bintree_10 AC 197 ms 35992 KiB
41_max_bintree_00 AC 250 ms 37288 KiB
41_max_bintree_01 AC 251 ms 37280 KiB
41_max_bintree_02 AC 249 ms 37336 KiB
41_max_bintree_03 AC 251 ms 37276 KiB
41_max_bintree_04 AC 253 ms 37276 KiB
90_challenge_00 AC 105 ms 32808 KiB
91_max_ans_00 AC 226 ms 55200 KiB