답안 #562078

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
562078 2022-05-14T05:14:44 Z Koosha_mv Zagrade (COI17_zagrade) C++14
0 / 100
3000 ms 64032 KB
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first

const int N=1e6+99;

int n,m,t,centroid,a[N],cnt[N],sz[N],vis[N];
pair<int,int> dp[N],pd[N];
ll ans;
string s;
vector<int> g[N];

void dfs1(int u,int p){
	sz[u]=1;
	for(auto v : g[u]){
		if(v==p || vis[v]) continue ;
		dfs1(v,u);
		sz[u]+=sz[v];
	}
	if(centroid==0 && 2*sz[u]>=m) centroid=u;
}
void dfs2(int u,int p){
	sz[u]=1;
	dp[u]=dp[p];
	if(s[u]=='(') dp[u].F++,dp[u].S=min(0,dp[u].S+1);
	else dp[u].F--,dp[u].S--;
	for(auto v : g[u]){
		if(v==p || vis[v]) continue ;
		dfs2(v,u);
		sz[u]+=sz[v];
	}
	if(dp[u].S==0){
		ans+=cnt[dp[u].F];
	}
	if(dp[u].F==0 && dp[u].S==0 && t==1){
		ans++;
	}
}
void dfs3(int u,int p){
	pd[u]=pd[p];
	if(s[u]==')') pd[u].F++,pd[u].S=min(0,pd[u].S+1);
	else pd[u].F--,pd[u].S--;
	for(auto v : g[u]){
		if(v==p || vis[v]) continue ;
		dfs3(v,u);
	}
	if(pd[u].S==0){
		cnt[pd[u].F]++;
	}
	if(s[centroid]=='(' && pd[u].F==1 && pd[u].S==0 && t==1){
		ans++;
	}
}
int calc(int u,int _t){
	t=_t;
	f(i,0,m+1) cnt[i]=0;
	pd[u]={0,0};
	if(s[u]=='(') dp[u]={1,0};
	else dp[u]={-1,-1};
	for(auto v : g[u]){
		if(vis[v]) continue ;
		dfs2(v,u);
		dfs3(v,u);
	}
}
void solve(int x,int _m){
	m=_m;
	centroid=0;
	dfs1(x,x);
	int rt=centroid;
	calc(rt,0); reverse(all(g[rt]));
	calc(rt,1);
	vis[rt]=1;
	for(auto v : g[rt]){
		if(vis[v]) continue ;
		//solve(v,sz[v]);
	}
}

int32_t main(){
	ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin>>n>>s;
	s=' '+s;
	f(i,1,n){
		int u,v;
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	solve(1,n);
	cout<<ans;
}

Compilation message

zagrade.cpp: In function 'int calc(int, int)':
zagrade.cpp:83:1: warning: no return statement in function returning non-void [-Wreturn-type]
   83 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 109 ms 48280 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3074 ms 64032 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 109 ms 48280 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -