QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#397779 | #5132. House Numbering | ucup-team1001# | RE | 134ms | 75024kb | C++23 | 3.9kb | 2024-04-24 16:55:53 | 2024-04-24 16:55:54 |
Judging History
answer
/*
Author: Haze
2024/4/24
*/
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(nullptr);
using namespace std;
using ll = long long;
#define int ll
#define debugs(x) cerr<<#x<<" "<<x<<endl;
//const int mod = 1000000000 + 7;
//const int itinf = 1000000999;
//const ll llinf = 2e18;
//const int N = 500099;
//
//3
//1 2 2
//2 3 9
//3 1 3
void solve() {
int n;
cin >> n;
// vector<vector<tuple<int, int, int>>> adj(n + 1);
// vector<vector<int>>
// to id w
vector<vector<tuple<int, int, int>>> adj(n * 2 + 1);
vector<set<int >> ans1(n * 2 + 1);
vector<int> arr1(n * 2 + 1);
vector<set<int >> ans2(n * 2 + 1);
vector<int> arr2(n * 2 + 1);
vector<int> vis(n * 2 + 1);
vector<int> du(n * 2 + 1);
for (int i = 1; i <= n; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].emplace_back(v, w, i);
adj[v].emplace_back(u, w, i);
du[u]++;
du[v]++;
}
stack<int> st;
for (int i = 1; i <= n; i++) {
if (du[i] == 1) {
st.emplace(i);
}
}
while (!st.empty()) {
auto t = st.top();
// if (t > n) {
// cout << -1 << endl;
// return;
// }
st.pop();
vis[t] = 1;
for (auto [i, w, id]: adj[t]) {
du[i]--;
if (du[i] == 1) {
arr1[id] = t;
ans1[t].emplace(1);
ans1[i].emplace(w);
arr2[id] = t;
ans2[t].emplace(1);
ans2[i].emplace(w);
st.emplace(i);
}
}
}
function<void(int, int, int)> dfs = [&](int pre, int now, int be) {
if (now > n) {
cout << -1 << endl;
return;
}
vis[now] = 1;
for (auto [i, w, id]: adj[now]) {
if (du[i] == 0 || i == pre) continue;
// cerr << now << " " << i << " " << w << " " << id << endl;
arr1[id] = now;
ans1[now].emplace(1);
ans1[i].emplace(w);
arr2[id] = i;
ans2[now].emplace(w);
ans2[i].emplace(1);
if (i != be) {
dfs(now, i, be);
}
}
};
for (int i = 1; i <= n; i++) {
if (du[i] != 0) {
for (auto [j, w, id]: adj[i]) {
if (du[j] != 0) {
arr1[id] = i;
ans1[i].emplace(1);
ans1[j].emplace(w);
arr2[id] = j;
ans2[i].emplace(w);
ans2[j].emplace(1);
dfs(i, j, i);
// return;
bool flag = 1;
for (int k = 1; k <= n; k++) {
if (ans1[k].size() != adj[k].size()) {
flag = 0;
}
}
if (flag) {
for (int k = 1; k <= n; k++) {
cout << arr1[k] << endl;
}
return;
}
flag = 1;
for (int k = 1; k <= n; k++) {
if (ans2[k].size() != adj[k].size()) {
flag = 0;
}
}
if (flag) {
for (int k = 1; k <= n; k++) {
cout << arr2[k] << endl;
}
return;
}
cout << "impossible" << endl;
return;
}
}
}
}
}
signed main() {
// IOS
int T = 1;
while (T--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3776kb
input:
3 1 2 2 2 3 9 3 1 3
output:
1 2 3
result:
ok
Test #2:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
4 1 2 2 1 3 2 2 3 2 1 4 2
output:
impossible
result:
ok
Test #3:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
3 3 2 2 2 1 2 1 3 2
output:
2 1 3
result:
ok
Test #4:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
3 1 3 374 3 2 519 2 1 549
output:
1 3 2
result:
ok
Test #5:
score: 0
Accepted
time: 2ms
memory: 4316kb
input:
1000 960 606 2 606 278 2 278 118 2 118 965 2 965 546 2 546 518 2 518 758 2 758 32 2 32 839 2 839 245 2 245 201 2 201 353 2 353 386 2 386 737 2 737 420 2 420 838 2 838 493 2 493 905 2 905 704 2 704 563 2 563 687 2 687 218 2 218 739 2 739 544 2 544 548 2 548 442 2 442 744 2 744 724 2 724 640 2 640 767...
output:
606 278 118 965 546 518 758 32 839 245 201 353 386 737 420 838 493 905 704 563 687 218 739 544 548 442 744 724 640 767 816 164 154 231 211 788 686 599 715 410 759 521 121 585 148 158 854 603 344 966 242 458 852 709 592 880 902 629 680 867 469 703 957 84 396 509 319 942 972 238 358 932 692 145 847 94...
result:
ok
Test #6:
score: 0
Accepted
time: 2ms
memory: 4296kb
input:
1000 758 943 361 943 940 927 940 267 37 267 932 61 932 503 811 503 851 961 851 759 727 759 553 953 553 327 963 327 766 814 766 146 790 146 219 456 219 751 418 751 594 395 594 355 506 355 800 556 800 487 448 487 154 412 154 152 12 152 418 767 418 538 525 538 353 651 353 964 296 964 346 516 346 139 14...
output:
943 940 267 932 503 851 759 553 327 766 146 219 751 594 355 800 487 154 152 418 538 353 964 346 139 6 527 485 836 814 24 118 537 603 409 573 589 848 492 394 973 960 279 955 531 291 611 995 864 384 632 464 870 507 561 968 583 969 432 404 510 296 840 285 998 141 727 637 466 802 477 111 592 630 434 793...
result:
ok
Test #7:
score: 0
Accepted
time: 10ms
memory: 10420kb
input:
10000 3186 2782 2 2782 298 2 298 5102 2 5102 7844 2 7844 2121 2 2121 1327 2 1327 963 2 963 1476 2 1476 6796 2 6796 2601 2 2601 3663 2 3663 537 2 537 7009 2 7009 8359 2 8359 9182 2 9182 3559 2 3559 2818 2 2818 3174 2 3174 1350 2 1350 3944 2 3944 4620 2 4620 7832 2 7832 7007 2 7007 8636 2 8636 131 2 1...
output:
2782 298 5102 7844 2121 1327 963 1476 6796 2601 3663 537 7009 8359 9182 3559 2818 3174 1350 3944 4620 7832 7007 8636 131 1774 2430 2023 4765 599 1384 7695 6664 5795 7263 2886 4772 1005 9465 4003 7943 5695 8458 9611 8537 1365 4295 9147 7347 4046 4230 6230 6636 7518 8450 1611 6158 5896 9895 8102 6003 ...
result:
ok
Test #8:
score: 0
Accepted
time: 4ms
memory: 10344kb
input:
10000 3852 8753 857 8753 6570 997 6570 8378 337 8378 6838 253 6838 6832 801 6832 1443 879 1443 5821 206 5821 9332 690 9332 481 638 481 4466 88 4466 9433 464 9433 1713 258 1713 5136 243 5136 1300 962 1300 4261 200 4261 599 974 599 6852 651 6852 7430 162 7430 6058 474 6058 1914 235 1914 6049 128 6049 ...
output:
8753 6570 8378 6838 6832 1443 5821 9332 481 4466 9433 1713 5136 1300 4261 599 6852 7430 6058 1914 6049 3935 9278 8520 3551 5041 4622 8693 4680 6652 2051 7185 6381 9164 7385 9627 7285 2680 3167 141 8850 9498 8900 1510 5471 2901 7697 5285 8881 6603 7404 1217 6444 6526 6581 6681 2582 7581 8204 3165 594...
result:
ok
Test #9:
score: 0
Accepted
time: 134ms
memory: 74916kb
input:
100000 49761 50860 2 50860 76368 2 76368 36060 2 36060 38900 2 38900 8477 2 8477 16539 2 16539 1834 2 1834 41645 2 41645 11420 2 11420 52011 2 52011 70194 2 70194 5318 2 5318 90571 2 90571 25209 2 25209 36177 2 36177 83455 2 83455 97871 2 97871 76892 2 76892 50332 2 50332 70903 2 70903 94195 2 94195...
output:
50860 76368 36060 38900 8477 16539 1834 41645 11420 52011 70194 5318 90571 25209 36177 83455 97871 76892 50332 70903 94195 61933 31177 21238 74820 99490 26047 31858 62948 29778 16892 94302 43724 37400 7932 2470 72838 51206 80051 39672 58583 92664 52368 19444 65620 76657 6056 6878 13492 61144 82351 2...
result:
ok
Test #10:
score: 0
Accepted
time: 112ms
memory: 75024kb
input:
100000 99597 2401 4114 2401 31522 8636 31522 5447 6268 5447 81737 4594 81737 33124 9354 33124 45703 4433 45703 11371 2069 11371 32023 3251 32023 75309 630 75309 41612 2093 41612 72484 2539 72484 68595 4051 68595 26179 3927 26179 32505 2939 32505 14017 7967 14017 41000 8122 41000 44698 9268 44698 135...
output:
2401 31522 5447 81737 33124 45703 11371 32023 75309 41612 72484 68595 26179 32505 14017 41000 44698 13554 81437 38670 29915 4115 11545 96469 82271 25158 40706 64753 16741 29158 27028 68763 67221 3642 20953 1842 44974 18774 77786 74748 32518 18966 94057 17449 81693 77323 66374 53595 14432 2837 57199 ...
result:
ok
Test #11:
score: -100
Runtime Error
input:
10 6 4 2 4 3 2 3 6 2 6 1 3 6 2 4 6 9 5 6 10 6 6 5 7 6 7 8 6 8 8