답안 #419716

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
419716 2021-06-07T11:51:24 Z NintsiChkhaidze Zagrade (COI17_zagrade) C++14
40 / 100
626 ms 64544 KB
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define left (node<<1),l,(l+r)>>1
#define right ((node<<1)|1),((l+r)>>1) + 1,r
using namespace std;
const int N = 300005;
int A[N],S[N],Q,dp[N],t[4*N],n;
ll res;
vector <int> v[N];
map <int,int> last;
void upd(int node,int l,int r,int idx,int val){
    if (l == r) {t[node] = val; return;}
    if (idx > ((l+r)>>1)) upd(right,idx,val);
    else upd(left,idx,val);
    t[node] = min(t[node<<1],t[((node<<1)|1)]);
}
int get(int node,int l,int r,int L,int R){
    if (r < L || R  < l) return 1e9;
    if (L <= l && r <= R) return t[node];
    int x = get(left,L,R),y = get(right,L,R);
    return min(x,y);
}
void dfs(int x,int par){
    A[x] = A[par] + S[x];
    upd(1,1,n,x,A[x]);
    
    if (last[A[x]]){
        int id = last[A[x]];
        int mn = get(1,1,n,id + 1,x) - A[x];
        if (mn == 0) dp[x] = dp[id] + 1,res+=dp[x];
    }
    else if (A[x] == 0){
        int mn = get(1,1,n,1,x);
        if (mn == 0) dp[x] = 1,res+=dp[x];
    }
    last[A[x]] = x;
    for (int j=0;j<v[x].size();j++)
        if (v[x][j]!=par) dfs(v[x][j],x);
}
void dfs2(int x,int par){
    A[x] = A[par] + S[x];
    upd(1,1,n,x,A[x]);
    
    if (last[A[x]]){
        int id = last[A[x]];
        int mn = get(1,1,n,x,id-1) - A[x];
        if (mn == 0) dp[x] = dp[id] + 1,res+=dp[x];
    }
    else if (A[x] == 0){
        int mn = get(1,1,n,x,n);
        if (mn == 0) dp[x] = 1,res+=dp[x];
    }
    last[A[x]] = x;
    for (int j=0;j<v[x].size();j++)
        if (v[x][j]!=par) dfs2(v[x][j],x);
}
void DFS(int x,int par){
    A[x] = A[par] + S[x];
    if (A[x] == 0) res++;
    if (A[x] < 0) return;
    for (int j=0;j<v[x].size();j++){
        int to = v[x][j];
        if (to!=par && A[to] >= 0) DFS(to,x);
    }
}
int main (){
    ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
    cin>>n;
    string s;
    cin>>s;
    
    s = '$' + s;
    for (int i=1;i<=n;i++)
        if (s[i] == '(') S[i] = 1;
        else S[i]= -1;
        
    for (int i=1;i<n;i++){
        int a,b;
        cin>>a>>b;
        v[a].pb(b),v[b].pb(a);
    }    

    if (n <= 1000){
        for (int i =1;i<=n;i++){
            if (S[i] == 1) DFS(i,i);
            for (int j=1;j<=n;j++)
                A[j] = 0;
        }
        cout<<res;
        return 0;
    }
    for (int i=1;i<=4*N;i++) 
        t[i] = 1e9;
        
    dfs(1,1);
    for (int i=1;i<=4*N;i++)
        t[i] = 1e9;
    for (int i=1;i<=n;i++) 
        dp[i] = 0,A[i]=0;
    
    last.clear();
    dfs2(n,n);
    cout<<res;
}

Compilation message

zagrade.cpp: In function 'void dfs(int, int)':
zagrade.cpp:38:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for (int j=0;j<v[x].size();j++)
      |                  ~^~~~~~~~~~~~
zagrade.cpp: In function 'void dfs2(int, int)':
zagrade.cpp:55:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |     for (int j=0;j<v[x].size();j++)
      |                  ~^~~~~~~~~~~~
zagrade.cpp: In function 'void DFS(int, int)':
zagrade.cpp:62:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |     for (int j=0;j<v[x].size();j++){
      |                  ~^~~~~~~~~~~~
zagrade.cpp: In function 'int main()':
zagrade.cpp:94:14: warning: iteration 1200019 invokes undefined behavior [-Waggressive-loop-optimizations]
   94 |         t[i] = 1e9;
      |         ~~~~~^~~~~
zagrade.cpp:93:19: note: within this loop
   93 |     for (int i=1;i<=4*N;i++)
      |                  ~^~~~~
zagrade.cpp:98:14: warning: iteration 1200019 invokes undefined behavior [-Waggressive-loop-optimizations]
   98 |         t[i] = 1e9;
      |         ~~~~~^~~~~
zagrade.cpp:97:19: note: within this loop
   97 |     for (int i=1;i<=4*N;i++)
      |                  ~^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 7372 KB Output is correct
2 Correct 16 ms 7436 KB Output is correct
3 Correct 12 ms 7372 KB Output is correct
4 Correct 9 ms 7372 KB Output is correct
5 Correct 10 ms 7372 KB Output is correct
6 Correct 16 ms 7440 KB Output is correct
7 Correct 15 ms 7372 KB Output is correct
8 Correct 7 ms 7432 KB Output is correct
9 Correct 11 ms 7372 KB Output is correct
10 Correct 9 ms 7372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 412 ms 54128 KB Output is correct
2 Correct 501 ms 63160 KB Output is correct
3 Correct 418 ms 57648 KB Output is correct
4 Correct 505 ms 61740 KB Output is correct
5 Correct 432 ms 57732 KB Output is correct
6 Correct 507 ms 59564 KB Output is correct
7 Correct 415 ms 57588 KB Output is correct
8 Correct 514 ms 59532 KB Output is correct
9 Correct 422 ms 57580 KB Output is correct
10 Correct 516 ms 64544 KB Output is correct
11 Correct 359 ms 57520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 7372 KB Output is correct
2 Correct 16 ms 7436 KB Output is correct
3 Correct 12 ms 7372 KB Output is correct
4 Correct 9 ms 7372 KB Output is correct
5 Correct 10 ms 7372 KB Output is correct
6 Correct 16 ms 7440 KB Output is correct
7 Correct 15 ms 7372 KB Output is correct
8 Correct 7 ms 7432 KB Output is correct
9 Correct 11 ms 7372 KB Output is correct
10 Correct 9 ms 7372 KB Output is correct
11 Correct 412 ms 54128 KB Output is correct
12 Correct 501 ms 63160 KB Output is correct
13 Correct 418 ms 57648 KB Output is correct
14 Correct 505 ms 61740 KB Output is correct
15 Correct 432 ms 57732 KB Output is correct
16 Correct 507 ms 59564 KB Output is correct
17 Correct 415 ms 57588 KB Output is correct
18 Correct 514 ms 59532 KB Output is correct
19 Correct 422 ms 57580 KB Output is correct
20 Correct 516 ms 64544 KB Output is correct
21 Correct 359 ms 57520 KB Output is correct
22 Incorrect 626 ms 26020 KB Output isn't correct
23 Halted 0 ms 0 KB -