結果

問題 No.1002 Twotone
ユーザー tsutajtsutaj
提出日時 2020-02-21 01:41:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,641 bytes
コンパイル時間 795 ms
コンパイル使用メモリ 69,304 KB
実行使用メモリ 14,016 KB
最終ジャッジ日時 2024-04-18 08:29:42
合計ジャッジ時間 8,090 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <algorithm>
#include <vector>
#include <tuple>
#include <utility>
#include <set>
using namespace std;
using Graph = vector< vector< pair<int, int> > >;

int main() {
    int N, K; scanf("%d%d", &N, &K);
    Graph G(N);
    for(int i=0; i+1<N; i++) {
        int u, v, c; scanf("%d%d%d", &u, &v, &c);
        u--; v--; c--;
        G[u].emplace_back(v, c);
        G[v].emplace_back(u, c);
    }

    vector<int> parent(N, -1), par_edge_color(N, -1);
    vector<int> depth(N);
    auto dfs = [&](auto&& f, int cur, int par) -> void {
        for(auto e : G[cur]) {
            int to, color; tie(to, color) = e;
            if(to == par) continue;
            depth[to] = depth[cur] + 1;
            parent[to] = cur;
            par_edge_color[to] = color;
            f(f, to, cur);
        }
    };

    dfs(dfs, 0, -1);
    long long int ans = 0;
    for(int i=0; i<N; i++) {
        for(int j=i+1; j<N; j++) {
            int u = i, v = j;
            set<int> S;
            while(u != v) {
                if(depth[u] != depth[v]) {
                    if(depth[u] > depth[v]) swap(u, v);
                    S.emplace(par_edge_color[v]);
                    v = parent[v];
                }
                else {
                    S.emplace(par_edge_color[u]);
                    S.emplace(par_edge_color[v]);
                    u = parent[u];
                    v = parent[v];
                }
            }
            if(S.size() == 2) {
                fprintf(stderr, "u = %d, v = %d\n", i+1, j+1);
                ans++;
            }
        }
    }
    printf("%lld\n", ans);
    return 0;
}
0