0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

深さ優先探索

Last updated at Posted at 2022-03-01

再帰により、深さを表現する

木以外でも使える()

int N, Q;
Graph E(201010); //つながり
int val[201010]; //総合得点

void dfs(int cu, int pa = -1){ //cuを訪問済み(-1)に設定 

    fore(to, E[cu]) //cuの最小全域木の頂点
        if (pa != to) { //訪問済みでないなら
        val[to] += val[cu];
        dfs(to, cu);
    }
}

int main() {
    cin >> N >> Q;//頂点数、辺数
    rep(i, 0, N - 1) {
        int a, b; cin >> a >> b; //繋がり
        a--; b--;
        E[a].push_back(b);
        E[b].push_back(a);
    }

    rep(q, 0, Q) {
        int p, x; cin >> p >> x; //pの最小全域木、x点
        p--;
        val[p] += x;
    }

    dfs(0);

    rep(i, 0, N) {
        if (i) printf(" ");
        printf("%d", val[i]);//表示
    }
    printf("\n");
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?