Submission #894923

#TimeUsernameProblemLanguageResultExecution timeMemory
894923vjudge1Zagrade (COI17_zagrade)C++11
10 / 100
133 ms31468 KiB
#include <bits/stdc++.h> #define pb push_back #define pf push_front using namespace std; #define F first #define S second typedef long long ll; #define pii pair <int, int> #define pll pair <ll, ll> typedef long double ld; const ll N = 3 * 1e5 + 10; const ll mod = 1e9 + 7; ll um(ll a, ll b){ return ((1LL * a * b) % mod + mod) % mod; } ll subr(ll a, ll b){ return ((1LL * a - b) % mod + mod) % mod; } ll add(ll a, ll b){ return (1LL * a + b) % mod; } ll binpow(ll x, ll step){ ll res = 1LL; while(step){ if(step & 1) res = um(res, x); x = um(x, x); step /= 2; } return res; } vector <int> q[N]; bool arr[N]; int ans, n; void over(){ int cur = 0, cnt = 1, plus = 0; map<int, int> mp; for(int i = 1; i <= n; i++){ if(arr[i]){ plus++; if(plus == 1) mp[cur + plus] = 1 + mp[cur]; else mp[cur + plus]++; } else{ plus = 0; cur++; ans += mp[cur]; } } cur = plus = 0; cnt = 1; map<int, int> mp1; for(int i = n; i >= 1; i--){ if(arr[i]){ plus++; if(plus == 1) mp1[cur + plus] = 1 + mp1[cur]; else mp1[cur + plus]++; } else{ plus = 0; cur++; ans += mp1[cur]; } } } void dfs(int v, int p, int cnt){ if(cnt == 0) ans++; for(auto u : q[v]){ if(u == p) continue; if(arr[u]) dfs(u, v, cnt + 1); else{ if(cnt > 0) dfs(u, v, cnt - 1); } } } void subtask1(){ for(int i = 1; i <= n; i++){ if(arr[i] == true) dfs(i, -1, 1); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); string s; cin >> n >> s; for(int i = 0; i < n; i++){ if(s[i] == '(') arr[i + 1] = true; } for(int i = 0, a, b; i < n - 1; i++){ cin >> a >> b; q[a].pb(b); q[b].pb(a); } if(n <= 1000){ subtask1(); } else over(); cout << ans; return 0; }

Compilation message (stderr)

zagrade.cpp: In function 'void over()':
zagrade.cpp:36:15: warning: variable 'cnt' set but not used [-Wunused-but-set-variable]
   36 |  int cur = 0, cnt = 1, plus = 0;
      |               ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...