답안 #636618

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
636618 2022-08-29T17:11:16 Z marlicu Zagrade (COI17_zagrade) C++14
100 / 100
421 ms 112812 KB
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second

typedef long long ll;
typedef pair <int, int> pii;

const int MAXN = 3e5 + 5;

int n;
char parentesi[MAXN];
vector <int> connessioni[MAXN];
map <int, int> piu[MAXN];
map <int, int> meno[MAXN];
int subalbero[MAXN];
int shift[MAXN];
int gruppo[MAXN];
ll risposta;

void collegare(int a, int b, int delta) {
  int nodo = b;

  if (subalbero[gruppo[a]] < subalbero[gruppo[b]]) swap(a, b);
  
  int& ga = gruppo[a]; 
  int& gb = gruppo[b];

  for (auto nx : piu[gb]) risposta += 1LL * nx.Y * meno[ga][-nx.X - shift[gb] - shift[ga]];
  for (auto nx : meno[gb]) risposta += 1LL * nx.Y * piu[ga][-nx.X - shift[gb] - shift[ga]];

  shift[gruppo[nodo]] += delta;

  for (auto nx : piu[gb]) piu[ga][nx.X + shift[gb] - shift[ga]] += nx.Y;
  for (auto nx : meno[gb]) meno[ga][nx.X + shift[gb] - shift[ga]] += nx.Y;

  piu[ga][-1 - shift[ga]] = 0;
  meno[ga][1 - shift[ga]] = 0;

  subalbero[ga] += subalbero[gb];

  /*
  cerr << a << ' ' << b << ' ' << delta << '\n';
  cerr << shift[ga] << ' ' << shift[gb] << ' ' << risposta << ' ' << nodo << '\n';
  cerr << "piu  "; for (auto nx : piu[ga]) cerr << nx.X << ' ' << nx.Y << " : "; cerr << '\n';
  cerr << "meno "; for (auto nx : meno[ga]) cerr << nx.X << ' ' << nx.Y << " : "; cerr << '\n';
  cerr << '\n';
  */

  gb = ga;
}

void dfs(int nodo, int pnodo) {
  subalbero[nodo] = 1;

  if (parentesi[nodo] == '(') piu[nodo][1]++;
  else meno[nodo][-1]++;

  for (auto nnodo : connessioni[nodo]) {
    if (nnodo == pnodo) continue;
    
    //cerr << "qui "<< nodo << ' ' << nnodo <<"\n";
    
    dfs(nnodo, nodo);
    collegare(nodo, nnodo, (parentesi[nodo] == '(' ? 1 : -1));
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> parentesi[i];
    gruppo[i] = i;
  }

  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;

    connessioni[u].push_back(v);
    connessioni[v].push_back(u);
  }

  dfs(0, -1);

  cout << risposta << '\n';

  return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 35668 KB Output is correct
2 Correct 17 ms 35668 KB Output is correct
3 Correct 16 ms 35652 KB Output is correct
4 Correct 17 ms 35668 KB Output is correct
5 Correct 17 ms 35668 KB Output is correct
6 Correct 17 ms 35668 KB Output is correct
7 Correct 17 ms 35628 KB Output is correct
8 Correct 17 ms 35668 KB Output is correct
9 Correct 19 ms 35732 KB Output is correct
10 Correct 17 ms 35668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 155 ms 94588 KB Output is correct
2 Correct 284 ms 110060 KB Output is correct
3 Correct 204 ms 98828 KB Output is correct
4 Correct 275 ms 107116 KB Output is correct
5 Correct 171 ms 99020 KB Output is correct
6 Correct 279 ms 102608 KB Output is correct
7 Correct 158 ms 98708 KB Output is correct
8 Correct 251 ms 102676 KB Output is correct
9 Correct 174 ms 98792 KB Output is correct
10 Correct 296 ms 112740 KB Output is correct
11 Correct 139 ms 98724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 35668 KB Output is correct
2 Correct 17 ms 35668 KB Output is correct
3 Correct 16 ms 35652 KB Output is correct
4 Correct 17 ms 35668 KB Output is correct
5 Correct 17 ms 35668 KB Output is correct
6 Correct 17 ms 35668 KB Output is correct
7 Correct 17 ms 35628 KB Output is correct
8 Correct 17 ms 35668 KB Output is correct
9 Correct 19 ms 35732 KB Output is correct
10 Correct 17 ms 35668 KB Output is correct
11 Correct 155 ms 94588 KB Output is correct
12 Correct 284 ms 110060 KB Output is correct
13 Correct 204 ms 98828 KB Output is correct
14 Correct 275 ms 107116 KB Output is correct
15 Correct 171 ms 99020 KB Output is correct
16 Correct 279 ms 102608 KB Output is correct
17 Correct 158 ms 98708 KB Output is correct
18 Correct 251 ms 102676 KB Output is correct
19 Correct 174 ms 98792 KB Output is correct
20 Correct 296 ms 112740 KB Output is correct
21 Correct 139 ms 98724 KB Output is correct
22 Correct 416 ms 89436 KB Output is correct
23 Correct 404 ms 90324 KB Output is correct
24 Correct 413 ms 90096 KB Output is correct
25 Correct 421 ms 90424 KB Output is correct
26 Correct 235 ms 95664 KB Output is correct
27 Correct 250 ms 93160 KB Output is correct
28 Correct 258 ms 91792 KB Output is correct
29 Correct 315 ms 112780 KB Output is correct
30 Correct 295 ms 112812 KB Output is correct
31 Correct 113 ms 66956 KB Output is correct
32 Correct 158 ms 98636 KB Output is correct