Submission #26339

# Submission time Handle Problem Language Result Execution time Memory
26339 2017-06-29T08:55:54 Z 알렉스(#1109) Zagrade (COI17_zagrade) C++14
100 / 100
1533 ms 47856 KB
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <tuple>

using namespace std;

char let[300010];
vector<int> arr[300010];
int par[300010];
bool chk[300010];

void dfs(int x, int c, int &mx, int &mxp)
{
    if(c > mx)
    {
        mx = c;
        mxp = x;
    }
    for(int y : arr[x])
    {
        if(chk[y] || y == par[x])
            continue;

        par[y] = x;
        dfs(y, c+1, mx, mxp);
    }
}

void dig(int x, int o1, int o2, int c1, int c2, vector<int> &vo, vector<int> &vc)
{
    if(let[x] == '(')
    {
        if(o2)
            o2--;
        else
            o1++;
        c2++;
    }
    else
    {
        if(c2)
            c2--;
        else
            c1++;
        o2++;
    }

    if(!o2)
        vo.push_back(o1);
    if(!c2)
        vc.push_back(c1);

    for(int y : arr[x])
    {
        if(chk[y] || y == par[x])
            continue;

        dig(y, o1, o2, c1, c2, vo, vc);
    }
}

long long f(int x)
{
    int d, p, q, c1, c2, i;
    long long r = 0;

    par[x] = x;
    d = 0;
    p = x;
    dfs(x, 0, d, p);
    x = p;

    par[x] = x;
    d = 0;
    p = x;
    dfs(x, 0, d, p);
    x = p;

    for(i = 0; i<d/2; i++)
        x = par[x];

    chk[x] = 1;
    // x is centroid
    par[x] = x;
    d = 0;
    p = x;
    dfs(x, 0, d, p);

    vector<int> open, close;
    if(let[x] == '(')
    {
        open.push_back(1);
        close.push_back(0);
    }
    else
    {
        open.push_back(0);
        close.push_back(1);
    }
    for(int y : arr[x])
    {
        if(chk[y])
            continue;

        vector<int> to, tc;
        dig(y, 0, 0, 0, 0, to, tc);
        if(let[x] == '(')
        {
            for(int &t: to)
                t++;
        }
        else
        {
            for(int &t: tc)
                t++;
        }
        sort(to.begin(), to.end());
        sort(tc.begin(), tc.end());
        p = 0;
        q = 0;
        for(i = 0;; i++)
        {
            while(p < to.size() && to[p] < i)
                p++;
            while(q < tc.size() && tc[q] < i)
                q++;
            if(p == to.size() || q == tc.size())
                break;
            if(to[p] != i || tc[q] != i)
                continue;
            c1 = c2 = 0;
            while(p < to.size() && to[p] == i)
            {
                p++;
                c1++;
            }
            while(q < tc.size() && tc[q] == i)
            {
                q++;
                c2++;
            }
            r -= 1LL * c1 * c2;
        }

        for(int t : to)
            open.push_back(t);
        for(int t : tc)
            close.push_back(t);
    }
    sort(open.begin(), open.end());
    sort(close.begin(), close.end());
    p = 0;
    q = 0;
    for(i = 0;; i++)
    {
        while(p < open.size() && open[p] < i)
            p++;
        while(q < close.size() && close[q] < i)
            q++;
        if(p == open.size() || q == close.size())
            break;
        if(open[p] != i || close[q] != i)
            continue;
        c1 = c2 = 0;
        while(p < open.size() && open[p] == i)
        {
            p++;
            c1++;
        }
        while(q < close.size() && close[q] == i)
        {
            q++;
            c2++;
        }
        r += 1LL * c1 * c2;
    }

    for(int y : arr[x])
    {
        if(chk[y])
            continue;
        r += f(y);
    }
    return r;
}

int main()
{
    int n, x, y, i;
    scanf("%d%s", &n, let+1);
    for(i = 0; i<n-1; i++)
    {
        scanf("%d%d", &x, &y);
        arr[x].push_back(y);
        arr[y].push_back(x);
    }
    printf("%lld", f(1));
    return 0;
}

Compilation message

zagrade.cpp: In function 'long long int f(int)':
zagrade.cpp:127:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(p < to.size() && to[p] < i)
                     ^
zagrade.cpp:129:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(q < tc.size() && tc[q] < i)
                     ^
zagrade.cpp:131:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if(p == to.size() || q == tc.size())
                  ^
zagrade.cpp:131:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if(p == to.size() || q == tc.size())
                                    ^
zagrade.cpp:136:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(p < to.size() && to[p] == i)
                     ^
zagrade.cpp:141:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(q < tc.size() && tc[q] == i)
                     ^
zagrade.cpp:160:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while(p < open.size() && open[p] < i)
                 ^
zagrade.cpp:162:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while(q < close.size() && close[q] < i)
                 ^
zagrade.cpp:164:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(p == open.size() || q == close.size())
              ^
zagrade.cpp:164:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(p == open.size() || q == close.size())
                                  ^
zagrade.cpp:169:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while(p < open.size() && open[p] == i)
                 ^
zagrade.cpp:174:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while(q < close.size() && close[q] == i)
                 ^
zagrade.cpp: In function 'int main()':
zagrade.cpp:194:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%s", &n, let+1);
                             ^
zagrade.cpp:197:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x, &y);
                              ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9964 KB Output is correct
2 Correct 3 ms 9964 KB Output is correct
3 Correct 6 ms 9964 KB Output is correct
4 Correct 3 ms 9964 KB Output is correct
5 Correct 3 ms 9964 KB Output is correct
6 Correct 0 ms 9964 KB Output is correct
7 Correct 3 ms 9964 KB Output is correct
8 Correct 3 ms 9964 KB Output is correct
9 Correct 0 ms 9964 KB Output is correct
10 Correct 0 ms 9964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 643 ms 42784 KB Output is correct
2 Correct 756 ms 45296 KB Output is correct
3 Correct 653 ms 42796 KB Output is correct
4 Correct 749 ms 45296 KB Output is correct
5 Correct 649 ms 42788 KB Output is correct
6 Correct 663 ms 44012 KB Output is correct
7 Correct 633 ms 42776 KB Output is correct
8 Correct 686 ms 44016 KB Output is correct
9 Correct 609 ms 42784 KB Output is correct
10 Correct 876 ms 47856 KB Output is correct
11 Correct 676 ms 47172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9964 KB Output is correct
2 Correct 3 ms 9964 KB Output is correct
3 Correct 6 ms 9964 KB Output is correct
4 Correct 3 ms 9964 KB Output is correct
5 Correct 3 ms 9964 KB Output is correct
6 Correct 0 ms 9964 KB Output is correct
7 Correct 3 ms 9964 KB Output is correct
8 Correct 3 ms 9964 KB Output is correct
9 Correct 0 ms 9964 KB Output is correct
10 Correct 0 ms 9964 KB Output is correct
11 Correct 643 ms 42784 KB Output is correct
12 Correct 756 ms 45296 KB Output is correct
13 Correct 653 ms 42796 KB Output is correct
14 Correct 749 ms 45296 KB Output is correct
15 Correct 649 ms 42788 KB Output is correct
16 Correct 663 ms 44012 KB Output is correct
17 Correct 633 ms 42776 KB Output is correct
18 Correct 686 ms 44016 KB Output is correct
19 Correct 609 ms 42784 KB Output is correct
20 Correct 876 ms 47856 KB Output is correct
21 Correct 676 ms 47172 KB Output is correct
22 Correct 1509 ms 21988 KB Output is correct
23 Correct 1153 ms 21288 KB Output is correct
24 Correct 1516 ms 21508 KB Output is correct
25 Correct 1533 ms 21576 KB Output is correct
26 Correct 793 ms 27300 KB Output is correct
27 Correct 769 ms 23868 KB Output is correct
28 Correct 846 ms 22532 KB Output is correct
29 Correct 916 ms 47852 KB Output is correct
30 Correct 909 ms 47856 KB Output is correct
31 Correct 189 ms 25060 KB Output is correct
32 Correct 723 ms 47176 KB Output is correct