結果

問題 No.650 行列木クエリ
ユーザー tanzakutanzaku
提出日時 2017-03-22 11:10:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,765 bytes
コンパイル時間 468 ms
コンパイル使用メモリ 51,208 KB
実行使用メモリ 5,352 KB
最終ジャッジ日時 2023-09-25 01:16:12
合計ジャッジ時間 2,976 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 21 ms
4,952 KB
testcase_02 RE -
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 21 ms
4,952 KB
testcase_05 RE -
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1,176 ms
5,352 KB
testcase_09 RE -
testcase_10 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <vector>

const int mod = (int)1e9+7;
const int MAX = 20000;

int n;
std::vector<int> g[MAX];
int es[MAX-1][2];
int par[MAX];
long long mat[MAX][4];
const long long id[4] = {1,0,0,1,};


void dfs(int v, int p) {
    par[v] = p;
    memcpy(mat[v], id, sizeof(id));
    for (int t : g[v]) if (t != p) dfs(t, v);
}

void merge(long long *a, long long *b) {
    long long t[] = {
        (a[0] * b[0] + a[1] * b[2]) % mod,
        (a[0] * b[1] + a[1] * b[3]) % mod,
        (a[2] * b[0] + a[3] * b[2]) % mod,
        (a[2] * b[1] + a[3] * b[3]) % mod,
    };
    memcpy(b, t, sizeof(t));
}

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        es[i][0] = a;
        es[i][1] = b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(0, -1);
    for (int i = 0; i < n - 1; i++) {
        if (par[es[i][0]] == es[i][1]) {
            std::swap(es[i][0], es[i][1]);
        }
    }
    int q;
    scanf("%d", &q);
    while (q-- > 0) {
        char c[2];
        scanf("%s", c);
        if (c[0] == 'x') {
            int j, x00, x01, x10, x11;
            scanf("%d%d%d%d%d", &j, &x00, &x01, &x10, &x11);
            const int v = es[j][1];
            mat[v][0] = x00;
            mat[v][1] = x01;
            mat[v][2] = x10;
            mat[v][3] = x11;
        } else if (c[0] == 'g') {
            int i, j;
            scanf("%d%d", &i, &j);
            long long ans[4] = {1,0,0,1,};
            for (int v = j; v != i; v = par[v]) {
                merge(mat[v], ans);
            }
            printf("%d %d %d %d\n", (int)ans[0], (int)ans[1], (int)ans[2], (int)ans[3]);
        } else {
            throw;
        }
    }
}
0