結果
問題 | No.439 チワワのなる木 |
ユーザー |
![]() |
提出日時 | 2016-11-12 03:04:12 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,432 bytes |
コンパイル時間 | 1,486 ms |
コンパイル使用メモリ | 162,512 KB |
実行使用メモリ | 33,744 KB |
最終ジャッジ日時 | 2024-11-25 18:57:13 |
合計ジャッジ時間 | 12,559 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 27 TLE * 1 |
コンパイルメッセージ
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); | ~~~~~^~~~~~~~~~~~~~~~~
ソースコード
#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; }