QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#736700#5132. House Numberingnicksms#WA 262ms57080kbC++173.7kb2024-11-12 12:43:202024-11-12 12:43:20

Judging History

你现在查看的是最新测评结果

  • [2024-11-12 12:43:20]
  • 评测
  • 测评结果:WA
  • 用时:262ms
  • 内存:57080kb
  • [2024-11-12 12:43:20]
  • 提交

answer

#include <bits/stdc++.h>
#define endln '\n'
#define print(x) cout << (x) << endln
using namespace std;
using ll = long long;

int n;
const int maxN = 1e5 + 5;
vector<pair<int, int>> connections[maxN];
map<int, int> ma[maxN];
set<int> adjacent[maxN];
set<pair<int, int>> directed;

struct Edge
{
    int u, v, w;
};

vector<Edge> input;
vector<int> cycle;
set<int> cycleSet;

bool dfs(int node, int from, set<int> &visited, stack<int> &stk)
{
    visited.insert(node);
    stk.push(node);
    for (pair<int, int> p : connections[node])
    {
        if (visited.count(p.first))
        {
            if (p.first != from)
            {
                while (stk.size() && stk.top() != p.first)
                {
                    cycle.push_back(stk.top());
                    stk.pop();
                }
                if (stk.size())
                    cycle.push_back(stk.top());
                return true;
            }
        }
        else
        {
            visited.insert(p.first);
            if (dfs(p.first, node, visited, stk))
            {
                return true;
            }
        }
    }
    stk.pop();
    return false;
}

void go(int node, int from)
{
    for (pair<int, int> p : connections[node])
    {
        if (cycleSet.count(p.first) == 0 && p.first != from)
        {
            directed.insert({p.first, node});
            go(p.first, node);
        }
    }
}

bool works()
{
    set<int> visited;
    for (int i = 0; i <= n; i++)
    {
        adjacent[i].clear();
    }
    for (int i = 0; i < cycle.size(); i++)
    {
        int cur = cycle[i];
        int nxt = cycle[(i + 1) % (cycle.size())];
        adjacent[nxt].insert(ma[cur][nxt]);
        visited.insert(cur);
    }
    for (int i = 0; i < cycle.size(); i++)
    {
        int node = cycle[i];
        for (pair<int, int> p : connections[node])
        {
            if (visited.count(p.first) == 0)
            {
                if (adjacent[node].count(p.second))
                {
                    return false;
                }
            }
        }
    }
    return true;
}

void solve()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int u, v, h;
        cin >> u >> v >> h;
        ma[u][v] = h;
        ma[v][u] = h;
        connections[u].push_back({v, h});
        connections[v].push_back({u, h});
        input.push_back({u, v, h});
    }
    set<int> visited;
    stack<int> stk;
    dfs(1, 0, visited, stk);
    for (int i : cycle)
    {
        cycleSet.insert(i);
    }

    for (int i : cycle)
    {
        go(i, -1);
    }

    if (works())
    {
        for (int i = 0; i < cycle.size(); i++)
        {
            int cur = cycle[i];
            int nxt = cycle[(i + 1) % (cycle.size())];
            directed.insert({cur, nxt});
        }
        for (Edge e : input)
        {
            if (directed.count({e.u, e.v}))
            {
                print(e.u);
            }
            else
            {
                print(e.v);
            }
        }
        return;
    }

    reverse(cycle.begin(), cycle.end());
    if (works())
    {
        for (int i = 0; i < cycle.size(); i++)
        {
            int cur = cycle[i];
            int nxt = cycle[(i + 1) % (cycle.size())];
            directed.insert({cur, nxt});
        }
        for (Edge e : input)
        {
            if (directed.count({e.u, e.v}))
            {
                print(e.u);
            }
            else
            {
                print(e.v);
            }
        }
        return;
    }
    print("impossible");
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 15232kb

input:

3
1 2 2
2 3 9
3 1 3

output:

2
3
1

result:

ok 

Test #2:

score: 0
Accepted
time: 4ms
memory: 15344kb

input:

4
1 2 2
1 3 2
2 3 2
1 4 2

output:

impossible

result:

ok 

Test #3:

score: 0
Accepted
time: 3ms
memory: 15236kb

input:

3
3 2 2
2 1 2
1 3 2

output:

3
2
1

result:

ok 

Test #4:

score: 0
Accepted
time: 0ms
memory: 15572kb

input:

3
1 3 374
3 2 519
2 1 549

output:

3
2
1

result:

ok 

Test #5:

score: 0
Accepted
time: 2ms
memory: 15788kb

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:

960
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
84...

result:

ok 

Test #6:

score: 0
Accepted
time: 3ms
memory: 15664kb

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:

758
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...

result:

ok 

Test #7:

score: 0
Accepted
time: 16ms
memory: 19464kb

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:

3186
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
...

result:

ok 

Test #8:

score: 0
Accepted
time: 12ms
memory: 19400kb

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:

3852
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
316...

result:

ok 

Test #9:

score: 0
Accepted
time: 234ms
memory: 57080kb

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:

49761
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
8...

result:

ok 

Test #10:

score: 0
Accepted
time: 262ms
memory: 56924kb

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:

99597
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
...

result:

ok 

Test #11:

score: -100
Wrong Answer
time: 0ms
memory: 15348kb

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

output:

4
3
6
1
2
9
10
5
7
8

result:

wrong answer Token "4" doesn't correspond to pattern "impossible"