結果

問題 No.439 チワワのなる木
ユーザー たこしたこし
提出日時 2016-11-12 03:04:12
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,432 bytes
コンパイル時間 1,487 ms
コンパイル使用メモリ 162,392 KB
実行使用メモリ 20,480 KB
最終ジャッジ日時 2024-05-04 09:02:30
合計ジャッジ時間 13,673 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,760 KB
testcase_01 AC 3 ms
5,760 KB
testcase_02 AC 4 ms
5,760 KB
testcase_03 AC 4 ms
5,888 KB
testcase_04 AC 4 ms
5,760 KB
testcase_05 AC 4 ms
5,632 KB
testcase_06 AC 4 ms
5,760 KB
testcase_07 AC 4 ms
5,760 KB
testcase_08 AC 4 ms
5,760 KB
testcase_09 AC 4 ms
5,888 KB
testcase_10 AC 3 ms
5,760 KB
testcase_11 AC 4 ms
5,760 KB
testcase_12 AC 4 ms
5,888 KB
testcase_13 AC 4 ms
5,888 KB
testcase_14 AC 4 ms
5,888 KB
testcase_15 AC 4 ms
5,888 KB
testcase_16 AC 4 ms
5,888 KB
testcase_17 AC 5 ms
5,888 KB
testcase_18 AC 56 ms
10,752 KB
testcase_19 AC 54 ms
10,240 KB
testcase_20 AC 94 ms
12,288 KB
testcase_21 AC 20 ms
7,808 KB
testcase_22 AC 19 ms
7,552 KB
testcase_23 AC 107 ms
13,568 KB
testcase_24 AC 123 ms
20,480 KB
testcase_25 AC 4,034 ms
11,284 KB
testcase_26 TLE -
testcase_27 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void input()’:
main.cpp:34:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   34 |     scanf("%d %d", &x, &y);
      |     ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
#define MAX_N 100005

int N;
string S;

class EdgeInfo{
public:
  int next, rev;
  LL w, ww;
  EdgeInfo(int aNext, int aRev)
  {
    next = aNext;
    rev = aRev;
    w = ww = -1;
  }
  EdgeInfo()
  {
    next = rev = w = ww = -1;
  }
};
vector<EdgeInfo> Edge[MAX_N];

void input()
{
  cin >> N;
  cin >> S;
  for (size_t i = 0; i < N-1; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    x--; y--;
    Edge[x].push_back(EdgeInfo(y, Edge[y].size()));
    Edge[y].push_back(EdgeInfo(x, Edge[x].size()-1));
  }
}

//w, ww
pair<LL, LL> dfs(int pre, int pos, LL &ans)
{
  pair<LL, LL> ret;
  if(S[pos] == 'w'){
    ret.first++;
  }
  for (int i = 0; i < Edge[pos].size(); i++) {
    EdgeInfo &edge = Edge[pos][i];
    if(edge.next == pre){
      continue;
    }
    pair<LL, LL> tmp;
    if(edge.w == -1){
      tmp = dfs(pos, edge.next, ans);
      edge.w = tmp.first;
      edge.ww = tmp.second;
    }
    else{
      tmp.first = edge.w;
      tmp.second = edge.ww;
    }

    ret.first += tmp.first;
    ret.second += tmp.second;
    if(S[pos] == 'w'){
      ret.second += tmp.first;
    }
    else if(S[pos] == 'c' && pre == -1){
      ans += tmp.second;
    }
  }
  return ret;
}

LL solve()
{
  LL ans = 0;
  for (size_t i = 0; i < S.length(); i++) {
    dfs(-1, i, ans);
  }
  return ans;
}

int main()
{
  input();
  cout << solve() << endl;
  return 0;
}
0