QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#745357 | #2918. Tree Number Generator | alexhamidi | WA | 988ms | 453768kb | C++14 | 2.0kb | 2024-11-14 09:25:57 | 2024-11-14 09:25:57 |
Judging History
answer
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
vector<int> digits(n+1);
vector<vector<string>> paths(n+1, vector<string>(n+1));
vector<vector<int>> con(n+1);
for (int i = 0; i < n-1; i++) {
int a, b;
cin >> a >> b;
for (auto& c : con[a]) {
if (paths[c][b].empty()) {
paths[c][b] = paths[c][a] + to_string(b);
paths[b][c] = to_string(a) + paths[a][c];
con[c].push_back(b);
con[b].push_back(c);
}
}
for (auto& c : con[b]) {
if (paths[c][a].empty()) {
paths[c][a] = paths[c][b];
paths[c][a] += to_string(a);
paths[a][c] = to_string(b);
paths[a][c] += paths[b][c];
con[c].push_back(a);
con[a].push_back(c);
}
}
paths[a][b] = to_string(b);
paths[b][a] = to_string(a);
con[b].push_back(a);
con[a].push_back(b);
}
for (int i = 1; i <= n; i++) {
paths[i][i] = to_string(i);
}
for (int i = 1; i <= n; i++) {
cin >> digits[i];
}
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
string path = paths[a][b];
int res = 0;
int l = path.size();
long long factor = 1;
for (int j = l-1; j >= 0; j--) {
int curr = digits[path[j]-'0'];
res = (res + (curr % m) * (factor % m)) % m;
factor = (factor * 10) % m;
}
cout << res << "\n";
}
return 0;
}
/*
simply simulate. problem - how to find the path to a node
need to be able to do it in logn
only one path - how to get that path?
need to have everything in the path to get the path
problem reduced to finding
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3620kb
input:
2 1 4 1 2 1 3 1 2 2 1 1 1 2 2
output:
0 0 0 0
result:
ok 4 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
3 10 5 1 2 2 3 0 0 0 1 3 3 1 2 2 2 3 2 1
output:
0 0 0 0 0
result:
ok 5 lines
Test #3:
score: -100
Wrong Answer
time: 988ms
memory: 453768kb
input:
2000 2 2000 937 1471 1672 937 356 1672 976 356 1257 356 1503 1257 783 1503 1783 937 1284 976 1955 1503 802 1257 583 1471 526 356 701 1783 393 1955 307 1955 386 1955 1070 937 1724 802 1397 1724 1140 937 422 526 1941 1955 1638 937 1469 526 1800 526 1035 1800 1009 1140 1195 1800 142 1471 1225 1469 1524...
output:
1 1 0 1 0 0 1 1 1 1 1 0 1 0 1 1 1 0 0 0 1 0 1 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1 1 0 1 1 1 0 0 1 1 1 1 1 0 1 0 1 1 1 0 0 1 0 1 1 0 1 0 0 0 1 1 1 0 1 0 0 1 1 0 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 1 0 0 1 0 1 1 1 1 0 0 1 1 1 0 1 1 ...
result:
wrong answer 1st lines differ - expected: '0', found: '1'