답안 #636617

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
636617 2022-08-29T17:08:38 Z marlicu Zagrade (COI17_zagrade) C++14
10 / 100
3000 ms 113356 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& ga = gruppo[a]; 
  int& gb = gruppo[b];

  int nodo = b;

  if (false && subalbero[ga] < subalbero[gb]) {
    swap(a, b);
    swap(ga, gb);
  }

  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 104 ms 35852 KB Output is correct
2 Correct 104 ms 35892 KB Output is correct
3 Correct 90 ms 35812 KB Output is correct
4 Correct 93 ms 35840 KB Output is correct
5 Correct 90 ms 35940 KB Output is correct
6 Correct 98 ms 35944 KB Output is correct
7 Correct 94 ms 35916 KB Output is correct
8 Correct 94 ms 35908 KB Output is correct
9 Correct 90 ms 35824 KB Output is correct
10 Correct 74 ms 35664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3047 ms 113356 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 35852 KB Output is correct
2 Correct 104 ms 35892 KB Output is correct
3 Correct 90 ms 35812 KB Output is correct
4 Correct 93 ms 35840 KB Output is correct
5 Correct 90 ms 35940 KB Output is correct
6 Correct 98 ms 35944 KB Output is correct
7 Correct 94 ms 35916 KB Output is correct
8 Correct 94 ms 35908 KB Output is correct
9 Correct 90 ms 35824 KB Output is correct
10 Correct 74 ms 35664 KB Output is correct
11 Execution timed out 3047 ms 113356 KB Time limit exceeded
12 Halted 0 ms 0 KB -