QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#260279#1195. One-Way Conveyorslemonilemon#TL 68ms8252kbC++204.6kb2023-11-21 23:40:322023-11-21 23:40:32

Judging History

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

  • [2023-11-21 23:40:32]
  • 评测
  • 测评结果:TL
  • 用时:68ms
  • 内存:8252kb
  • [2023-11-21 23:40:32]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#ifdef genshin
#include <experimental/iterator>
#endif
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using uint = unsigned int;
const double EPS = 1e-8;
const int INF = 0x3F3F3F3F;
const ll LINF = 46 * ll(1e17);
const int MOD = 1e9 + 7;
const int maxn = 1e5 + 25;

namespace bridge_cc {
    vector<int> tim, low;
    stack<int, vector<int>> st;
    int t, bcc_id;
    void dfs(int u, int p, const vector<vector<pair<int, int>>> &edge, vector<int> &pa) {
        tim[u] = low[u] = t++;
        st.push(u);
        for (const auto &[v, id] : edge[u]) {
            if (id == p)
                continue;
            if (tim[v])
                low[u] = min(low[u], tim[v]);
            else {
                dfs(v, id, edge, pa);
                if (low[v] > tim[u]) {
                    int x;
                    do {
                        pa[x = st.top()] = bcc_id;
                        st.pop();
                    } while (x != v);
                    bcc_id++;
                }
                else 
                    low[u] = min(low[u], low[v]);
            }
        }
    }
    vector<int> solve(const vector<vector<pair<int, int>>> &edge) {
        int n = edge.size();
        tim.resize(n);
        low.resize(n);
        t = bcc_id = 1;
        vector<int> pa(n);

        for (int i = 0; i < n; i++) {
            if (!tim[i]) {
                dfs(i, -1, edge, pa);
                while (!st.empty()) {
                    pa[st.top()] = bcc_id;
                    st.pop();
                }
                bcc_id++;
            }
        }
        return pa;
    }
};

signed main() { ios::sync_with_stdio(0); cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> edges(m);
    vector<vector<pair<int, int>>> edge(n);
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        edge[u].push_back({v, i});
        edge[v].push_back({u, i});
        edges[i] = {u, v};
    }
    auto pa = bridge_cc::solve(edge);    

    int t = *max_element(pa.begin(), pa.end());
    vector<vector<pair<int, int>>> nw_edge(t + 1);
    vector<vector<pair<int, int>>> eve(t + 1);
    map<pair<int, int>, pair<int, int>> ori;

    for (int i = 0; i < m; i++) {
        auto [u, v] = edges[i];
        auto [a, b] = edges[i];
        u = pa[u];
        v = pa[v];
        if (u == v)
            continue;
        nw_edge[u].push_back({v, i});
        nw_edge[v].push_back({u, i + m});
        ori[{u, v}] = {a, b};
        ori[{v, u}] = {b, a};
    }

    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        a = pa[a];
        b = pa[b];
        eve[a].push_back({i, 1});
        eve[b].push_back({i, -1});
    }

    vector<bool> vis(t + 1);
    bool ok = true;
    vector<pair<int, int>> ans;
    function<map<int, int>(int)> dfs = [&] (int u) {
        vis[u] = true;
        map<int, int> has;
        for (const auto &[tar, dir] : eve[u]) {
            if (has.count(tar))
                has.erase(tar);
            else
                has[tar] = dir;
        }
        for (const auto &[v, i] : nw_edge[u]) {
            if (vis[v])
                continue;
            auto owo = dfs(v);
            int cur = 0;
            for (const auto &[tar, dir]: owo) {
                if (!cur)
                    cur = dir;
                else {
                    if (cur != dir)
                        ok = false;
                }
                if (has.count(tar))
                    has.erase(tar);
                else
                    has[tar] = dir;
            }
            auto [a, b] = ori[{u, v}];
            if (cur == 1)
                swap(a, b);
            ans.push_back({a, b});
        }
        return has;
    };
    for (int i = 1; i <= t; i++)
        if (!vis[i])
            dfs(i);

    if (!ok) {
        cout << "No\n";
        return 0;
    }

    vector<int> tim(n);
    int num = 1;
    function<void(int)> solve = [&] (int u) {
        tim[u] = num++;
        for (const auto &[v, i] : edge[u]) {
            if (pa[v] != pa[u])
                continue;
            if (tim[v] && tim[v] < tim[u])
                continue;
            if (tim[v]) {
                ans.push_back({v, u});
            }
            else {
                solve(v);
                ans.push_back({u, v});
            }
        }
    };
    for (int i = 0; i < n; i++)
        if (!tim[i])
            solve(i);

    cout << "Yes\n";
    for (const auto &[a, b] : ans)
        cout << a + 1 << ' ' << b + 1 << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3384kb

input:

3 2
1 2
2 3
3
1 2
1 3
2 3

output:

Yes
1 2
2 3

result:

ok OK!

Test #2:

score: 0
Accepted
time: 1ms
memory: 3508kb

input:

3 2
1 2
2 3
3
1 2
1 3
3 2

output:

No

result:

ok OK!

Test #3:

score: 0
Accepted
time: 1ms
memory: 3420kb

input:

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

output:

Yes
1 4
2 3
1 2
3 1

result:

ok OK!

Test #4:

score: 0
Accepted
time: 32ms
memory: 6644kb

input:

9990 47670
134 2450
4227 9100
1861 9948
120 8957
1161 4767
76 1714
5207 5838
2726 8974
2489 8619
2379 4933
3675 9554
344 3215
1654 6168
5630 6614
3247 5202
4456 5373
4380 6605
2626 4064
2194 6332
2749 9719
360 5059
7967 8835
5433 6494
497 6638
5945 9063
7576 7879
3550 4107
83 2891
3107 6917
2089 689...

output:

No

result:

ok OK!

Test #5:

score: 0
Accepted
time: 68ms
memory: 8252kb

input:

3219 13421
762 2133
1106 2880
2287 2476
313 992
741 1903
811 2050
1468 2184
3031 3037
403 1855
1060 1415
1792 2804
772 2956
140 2281
808 1953
895 1731
1217 1551
1264 1885
749 1082
1564 2824
1549 1741
1966 2730
112 2825
158 2625
2128 2917
128 3032
644 3194
1634 2743
1545 1961
2402 2680
2663 2814
1040...

output:

No

result:

ok OK!

Test #6:

score: 0
Accepted
time: 29ms
memory: 5332kb

input:

8013 18891
6127 6374
3969 4617
215 2699
268 6282
167 5415
1318 6256
3994 6192
4325 7186
1785 4188
6570 7153
772 5901
9 5190
873 6509
161 7476
2960 2966
4598 7170
1157 3798
758 4508
1446 5205
445 5989
686 5479
669 4711
6254 6860
1722 7020
463 3494
5063 7309
2231 6762
1208 4304
4789 5081
3925 6248
107...

output:

No

result:

ok OK!

Test #7:

score: 0
Accepted
time: 21ms
memory: 6328kb

input:

9968 46595
3655 5544
5747 9340
6020 9528
5789 9882
4609 8832
1969 5167
2610 8012
324 9387
694 3647
3667 8483
4202 4963
2643 8104
1139 9679
1407 7022
9031 9944
5183 8744
3341 3858
326 2610
414 1317
657 7942
4702 5671
2072 3200
3074 3597
26 3728
288 7757
144 9580
1374 2065
2683 8068
1341 6526
2140 257...

output:

Yes
9692 2722
3962 7975
7658 1074
4190 6076
6843 5284
6429 8223
3767 2557
3 38
6279 4042
6907 4029
9643 1819
9543 5341
9276 9172
8630 2127
7755 9211
9276 305
6179 5097
1960 7338
3413 4826
656 5776
1575 5015
941 7682
7372 1325
2624 6387
3843 4405
8458 7337
1506 2770
5427 6897
3936 5700
4587 94
1463 1...

result:

ok OK!

Test #8:

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

input:

273 1157
172 181
175 181
180 224
42 164
196 252
126 183
32 225
44 138
55 203
71 193
97 180
190 217
229 259
49 221
53 71
32 125
122 168
98 147
209 261
6 245
36 214
134 253
205 223
25 168
167 236
35 164
71 87
7 170
13 36
116 132
147 165
167 254
122 179
83 97
78 172
107 127
12 59
15 242
48 201
225 269
...

output:

Yes
118 242
255 240
93 131
120 145
105 54
6 245
59 12
59 204
68 59
12 68
204 68
201 68
204 201
59 201
12 201
48 201
68 48
204 48
59 48
12 48
208 48
68 208
204 208
12 208
201 208
59 208
153 208
12 153
204 153
68 153
201 153
59 153
48 153
118 153
12 118
204 118
59 118
48 118
68 118
1 118
59 1
12 1
68 ...

result:

ok OK!

Test #9:

score: 0
Accepted
time: 7ms
memory: 5024kb

input:

7563 11902
923 6491
841 5346
4236 6858
382 7479
3348 6098
1908 5546
564 6550
4767 6536
5181 7272
979 4282
3250 4044
4296 4678
57 5608
1172 3104
849 4089
1936 2337
4111 5978
2091 4188
5286 5313
3866 4426
6111 6585
970 2002
1477 2355
3425 4650
2892 2912
2554 6195
2830 6120
384 3107
4271 5234
629 1319
...

output:

Yes
6279 63
6279 5545
3819 6279
7375 1979
7375 6568
7375 6471
7200 3387
4105 5963
4105 6871
2575 4105
850 7448
850 7207
7025 850
342 3011
4593 163
4593 6275
5768 4593
5860 465
465 2477
465 5768
5768 2884
5269 2172
2328 2980
2328 2086
7416 2328
3068 4597
3068 2668
7416 3068
830 3267
7338 1168
7338 13...

result:

ok OK!

Test #10:

score: -100
Time Limit Exceeded

input:

8925 13782
1859 2185
5433 7565
6107 7778
1699 5422
2247 5228
3048 6500
996 8342
1063 7629
5648 6266
1827 3051
761 1875
8268 8811
1770 5354
400 8680
1877 7156
2037 3933
1393 6095
904 3022
5109 8078
6775 8153
1612 4288
484 1918
4339 4349
3907 6395
2832 5106
4928 6441
1572 6969
1255 4539
5433 8335
1760...

output:


result: