QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#833612#853. Flat Organizationucup-team5071#AC ✓1258ms203800kbC++204.2kb2024-12-26 22:02:282024-12-26 22:02:28

Judging History

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

  • [2024-12-26 22:02:28]
  • 评测
  • 测评结果:AC
  • 用时:1258ms
  • 内存:203800kb
  • [2024-12-26 22:02:28]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> p)
{
    out << "(" << p.first << "," << p.second << ")";
    return out;
}
template <typename T>
ostream &operator<<(ostream &out, vector<T> v)
{
    out << "[";
    if (!v.empty())
        cout << v[0];
    for (int i = 1; i < (int)v.size(); i++)
        out << "," << v[i];
    out << "]";
    return out;
}
typedef long long ll;
const ll inf = 1e18;
int gmin(ll &x, ll y)
{
    if (y < x)
    {
        x = y;
        return 1;
    }
    return 0;
}
struct info
{
    int x, p, q;
};
void solve()
{
    int n;
    cin >> n;
    vector<vector<pair<int, ll>>> ve(n + 1);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            ll x;
            cin >> x;
            if (x > 0)
                ve[i].emplace_back(j, x);
        }
    }
    if (n == 1)
    {
        cout << "YES\n0 0\n";
        return;
    }
    if (n == 2)
    {
        cout << "NO\n";
        return;
    }
    stack<int> s;
    int num = 0, dfstime = 0;
    vector<int> col(n + 1), dfn(n + 1), low(n + 1);
    auto tarjan = [&](auto &self, int u) -> void
    {
        s.push(u);
        dfn[u] = low[u] = ++dfstime;
        for (auto [v, len] : ve[u])
        {
            if (!dfn[v])
            {
                self(self, v);
                low[u] = min(low[u], low[v]);
            }
            else if (!col[v])
                low[u] = min(low[u], dfn[v]);
        }
        if (dfn[u] == low[u])
        {
            col[u] = ++num;
            while (s.top() != u)
            {
                col[s.top()] = num;
                s.pop();
            }
            s.pop();
        }
    };
    for (int i = 1; i <= n; i++)
        if (!dfn[i])
            tarjan(tarjan, i);
    vector<vector<pair<int, ll>>> ve2(num + 1);
    vector<vector<tuple<int, ll, int, int>>> ve3(num + 1);
    vector<int> cnt(num + 1);
    for (int i = 1; i <= n; i++)
    {
        for (auto &[to, len] : ve[i])
            if (col[i] != col[to])
            {
                ve2[col[i]].emplace_back(col[to], len);
                ve3[col[to]].emplace_back(col[i], len, i, to);
                cnt[col[to]]++;
            }
    }
    vector<int> lis{0};
    {
        queue<int> qu;
        for (int i = 1; i <= num; i++)
            if (cnt[i] == 0)
                qu.push(i);
        while (!qu.empty())
        {
            int x = qu.front();
            qu.pop();
            lis.push_back(x);
            for (auto &[to, len] : ve2[x])
            {
                if (--cnt[to] == 0)
                    qu.push(to);
            }
        }
    }
    assert((int)lis.size() == num + 1);
    vector<int> id(num + 1);
    for (int i = 1; i <= num; i++)
        id[lis[i]] = i;
    vector<ll> dp(num + 1, inf);
    vector<info> pr(num + 1);
    vector<vector<pair<ll, int>>> mi(num + 1, vector<pair<ll, int>>(num + 1, make_pair(inf, 0)));
    dp[1] = 0;
    mi[1][1] = make_pair(0, 1);
    // cout << "col =" << col << endl;
    // cout << "lis =" << lis << endl;
    for (int i = 2; i <= num; i++)
    {
        for (auto [to, len, p, q] : ve3[lis[i]])
        {
            to = id[to];
            // cout << "i=" << i << " to=" << to << " len=" << len << " p=" << p << " q=" << q << endl;
            // cout << "v=" << mi[to][i - 1].first + len << endl;
            if (gmin(dp[i], mi[to][i - 1].first + len))
            {
                pr[i] = info{mi[to][i - 1].second, p, q};
            }
        }
        mi[i][i] = {dp[i], i};
        for (int j = i - 1; j >= 1; j--)
            mi[j][i] = min(mi[j][i - 1], mi[j + 1][i]);
    }
    int now = num;
    vector<pair<int, int>> ans;
    while (now != 1)
    {
        ans.emplace_back(pr[now].p, pr[now].q);
        now = pr[now].x;
    }
    cout << "YES\n";
    cout << ans.size() << " " << dp[num] << "\n";
    reverse(ans.begin(), ans.end());
    for (auto [x, y] : ans)
        cout << x << " " << y << "\n";
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--)
        solve();
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3576kb

input:

1
5
0 1 0 6 11
0 0 1 6 12
2 0 0 7 12
0 0 0 0 4
0 0 0 0 0

output:

YES
2 10
1 4
4 5

result:

ok OK!

Test #2:

score: 0
Accepted
time: 15ms
memory: 3856kb

input:

100
5
0 1 0 6 11
0 0 1 6 12
2 0 0 7 12
0 0 0 0 4
0 0 0 0 0
1
0
2
0 0
7 0
2
0 1000000000
0 0
3
0 0 5
6 0 0
0 7 0
3
0 4 6
0 0 0
0 1 0
3
0 4 0
0 0 0
6 3 0
3
0 0 0
10 0 6
3 0 0
3
0 1999999998 1999999999
0 0 1999999998
0 0 0
50
0 105800 801 1800 64800 0 259200 288801 72201 12801 125000 20001 28801 33800 ...

output:

YES
2 10
1 4
4 5
YES
0 0
NO
NO
YES
0 0
YES
1 4
1 2
YES
1 3
3 2
YES
2 9
2 3
3 1
YES
1 1999999999
1 3
YES
3 602
4 33
11 47
34 39
YES
3 649
27 12
32 45
17 29
YES
5 1209
1 18
14 35
35 4
4 25
25 12
YES
3 844
3 8
1 41
14 21
YES
3 866
17 35
35 44
46 26
YES
4 1066
10 28
36 24
24 2
37 8
YES
3 1122
4 17
43 19...

result:

ok OK!

Test #3:

score: 0
Accepted
time: 110ms
memory: 5088kb

input:

50
200
0 40000001 47000001 35500000 37500501 0 0 0 26000000 29500501 0 22000501 6000000 33000000 0 73500000 68500501 50500500 32000000 12000001 0 0 49000000 0 67000500 70000000 5500500 60500001 0 28000001 35000500 31000001 0 36500001 69500000 57000001 65500000 63500000 0 64000500 51000001 56500000 6...

output:

YES
30 48002011
125 39
78 174
174 167
167 148
148 29
29 93
22 21
195 1
183 13
13 71
60 20
20 170
124 63
132 110
173 97
97 120
72 142
142 10
10 52
32 54
95 4
4 51
2 164
23 123
36 135
73 57
150 37
25 138
26 47
112 85
YES
30 42342994
114 109
146 162
79 29
72 103
166 121
32 155
143 77
50 173
173 26
28 7...

result:

ok OK!

Test #4:

score: 0
Accepted
time: 1220ms
memory: 198184kb

input:

5
2000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

YES
2 1023134
1609 106
1628 1
YES
2 1792364
1445 1040
594 1232
YES
325 416316941
532 682
1692 1737
1737 414
1682 715
538 254
1850 1994
1994 73
914 688
1947 1566
1566 1177
519 10
203 1577
1330 721
1287 786
786 1686
1291 1918
1773 155
1936 1911
1911 1522
1119 584
584 656
1851 1250
1020 417
1830 189
18...

result:

ok OK!

Test #5:

score: 0
Accepted
time: 1218ms
memory: 203740kb

input:

5
2000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

YES
29 996500000
1609 1123
1123 53
53 137
137 21
21 9
9 600
1661 1378
1378 1667
1667 1632
1632 260
260 963
1559 1755
1755 78
78 1353
1353 241
241 166
166 149
149 113
113 58
58 10
10 56
56 1040
1910 1322
1322 819
819 885
885 491
491 17
17 8
8 1
YES
13 994553657
324 19
19 1668
1825 1216
1216 912
912 9...

result:

ok OK!

Test #6:

score: 0
Accepted
time: 1258ms
memory: 198680kb

input:

5
2000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2289820 14688249 0 0 0 0 9856840 4321803 39208 583215 0 0 0 0 0 0 0 0 0 0 0 10857808 0 0 0 0 0 0 0 11712810 0 0 0 0 0 5848230 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11045028 0 0 5712236 72208 9245030 0 0 0 0 0 0 0 10580003 18361832 0 0 20995247 0 24081817 0 0 0 27...

output:

YES
1999 449308
161 1784
1784 659
659 1794
1794 1490
1490 707
707 384
384 432
432 1643
1643 1850
1850 510
510 992
992 721
721 241
241 166
166 1518
1518 230
230 1254
1254 607
607 736
736 126
126 1262
1262 1929
1929 1499
1499 182
182 1803
1803 787
787 410
410 626
626 1856
1856 1406
1406 484
484 844
84...

result:

ok OK!

Test #7:

score: 0
Accepted
time: 1257ms
memory: 198688kb

input:

5
2000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 306240 4975575 0 0 0 0 2735274 794096 689 39371 0 0 0 0 0 0 0 0 0 0 0 3162279 0 0 0 0 0 0 0 3543126 0 0 0 0 0 1250019 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3244419 0 0 1206671 1713 2484549 0 0 0 0 0 0 0 3041751 6954466 0 0 8503070 0 10445400 0 0 0 12655 512079 0 ...

output:

YES
978 6111
161 659
659 1490
1490 432
384 1643
1643 1850
1850 721
721 1518
1518 607
1254 126
126 1499
1499 182
182 410
410 1856
1856 1406
1406 844
844 203
203 1457
1457 1764
1764 1105
1105 947
947 705
705 1195
1195 1146
1146 1384
1384 944
944 485
485 36
36 1537
1401 118
118 1942
1942 290
290 1935
1...

result:

ok OK!

Test #8:

score: 0
Accepted
time: 871ms
memory: 203800kb

input:

45
2000
0 0 0 7 0 7 0 0 0 0 0 0 0 0 0 7 0 0 0 7 7 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 7 0 0 0 7 7 0 0 0 7 7 7 7 0 7 0 0 7 0 0 0 0 0 7 7 0 0 0 7 7 7 7 0 7 0 0 0 7 0 7 0 0 7 0 0 0 0 0 0 0 7 7 7 0 7 7 0 7 7 0 0 0 0 7 0 0 0 0 0 7 7 7 0 0 7 0 0 0 7 0 7 0 0 0 7 0 0 0 0 7 7 0 0 7 0 0 0 0 0 7 0 7 0 0 0 7 ...

output:

YES
2 21
1851 1
1 946
YES
2 21
1184 1
1 1401
YES
2 21
87 1
1 65
YES
2 21
10 1
1 2
YES
2 21
2 1
1 5
YES
2 21
13 1
1 2
YES
2 21
1 4
4 13
YES
2 21
3 1
1 15
YES
2 21
2 1
1 11
YES
2 21
5 3
3 7
YES
2 21
9 3
3 1
YES
2 21
9 2
2 11
YES
2 21
2 3
3 5
YES
2 21
5 1
1 11
YES
2 21
1 8
8 3
YES
2 21
6 1
1 16
YES
2 2...

result:

ok OK!