QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#870171#4809. Maximum RangeXuYuemingWA 22ms12204kbC++209.4kb2025-01-25 15:06:502025-01-25 15:06:50

Judging History

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

  • [2025-01-25 15:06:50]
  • 评测
  • 测评结果:WA
  • 用时:22ms
  • 内存:12204kb
  • [2025-01-25 15:06:50]
  • 提交

answer

#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <queue>
using namespace std;

const int N = 100010, M = 100010;

struct node {
    int to, nxt, len;
} edge[M << 1];
int head[N], tot = 1;
void add(int u, int v, int w) {
    edge[++tot] = {
        .to = v, .nxt = head[u], .len = w
    }, head[u] = tot;
}

int n, m;

int dfn[N], low[N], timer;
int stack[N], top;
int scnt, sccno[N];

void tarjan(int u, int f) {
    dfn[u] = low[u] = ++timer;
    stack[++top] = u;
    for (int i = head[u]; i; i = edge[i].nxt) {
        if ((i ^ f) == 1) continue;
        int v = edge[i].to;
        if (!dfn[v]) {
            tarjan(v, i);
            low[u] = min(low[u], low[v]);
        } else {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if (low[u] == dfn[u]) {
        ++scnt;
        do {
            int v = stack[top--];
            sccno[v] = scnt;
            // printf("%d: %d\n", scnt, v);
        } while (stack[top + 1] != u);
    }
}

int mx[N], mi[N], xp[N], ip[N];

// bool ins[N << 1], vis[N];
// bool dfs(int u, int fr, vector<int>& vec, int target) {
//     vis[u] = true;
//     if (u == target) return vec.emplace_back(u), true;
//     for (int i = head[u]; i; i = edge[i].nxt) {
//         if (i == (fr ^ 1)) continue;
//         if (ins[i]) continue;
//         int v = edge[i].to;
//         if (vis[v]) continue;
//         bool res = dfs(v, i, vec, target);
//         if (res) {
//             ins[i] = ins[i ^ 1] = true;
//             vec.emplace_back(u);
//             return true;
//         }
//     }
//     return false;
// }

vector<int> ans;
int xxx, iii, u1, u2, v1, v2;


// bool intree[M << 1], vis[N];
// int FA[N], DOWN;
// void dfs(int u, int fr) {
//     vis[u] = true;
//     if (u == v1) FA[u1] = u, dfs(u1, xxx);
//     if (u == u2 && fr != iii) FA[v2] = u, DOWN = v2, dfs(v2, iii ^ 1);
//     if (u == v2 && fr != (iii ^ 1)) FA[u2] = u, DOWN = u2, dfs(u2, iii);
//     for (int i = head[u]; i; i = edge[i].nxt) {
//         if (i == (fr ^ 1)) continue;
//         if (i == (xxx ^ 1)) continue;
//         if (i == iii) continue;
//         if (i == (iii ^ 1)) continue;
//         int v = edge[i].to;
//         if (vis[v]) continue;
//         FA[v] = u;
//         intree[i] = intree[i ^ 1] = true;
//         dfs(v, i);
//     }
// }

// bool can[N];
// int fff[N];

// bool dfs(int u) {
//     if (can[u]) {
//         // printf("fff[5] = %d\n", fff[5]);
//         // printf("fff[4] = %d\n", fff[4]);
//         for (int x = u; x; x = fff[x])
//             ans.emplace_back(x);
//         reverse(ans.begin(), ans.end());
//         return true;
//     }
//     for (int i = head[u]; i; i = edge[i].nxt) {
//         if (!intree[i]) continue;
//         int v = edge[i].to;
//         if (v == FA[u]) continue;
//         bool rr = dfs(v);
//         if (rr) {
//             ans.emplace_back(u);
//             return true;
//         }
//     }
//     return false;
// }

struct Net_Max_Flow {
	struct Graph {
		struct node {
			int to, flow, cap, nxt;
		} edge[M << 3];
		int tot = 1, head[N];
		inline void add(int u, int v, int w) {
			edge[++tot] = {v, 0, w, head[u]};
			head[u] = tot;
		}
		inline node & operator [] (const int x) {
			return edge[x];
		}
	} xym;
	
	int n;
	
	inline void init(int _n) {
		n = _n, xym.tot = 1;
		memset(xym.head + 1, 0x00, sizeof (int) * n);
	}
	
	inline void add(int u, int v, int fl, bool tag = false) {
		if (!tag) xym.add(u, v, fl), xym.add(v, u, 0);
        else {
            xym.add(u, v, fl), xym.add(v, u, fl);
        }
	}
    
    bool vis[N];
	
    int flow(int u, int T, int lim) {
        if (u == T) return lim;
        vis[u] = true;
        for (int i = xym.head[u]; i; i = xym.edge[i].nxt)
            if (xym.edge[i].flow < xym.edge[i].cap) {
                int v = xym.edge[i].to;
                if (!vis[v] && flow(v, T, lim))
                    return ++xym.edge[i].flow, --xym.edge[i ^ 1].flow, 1;
            }
        return 0;
    }
	
	long long solve(int S, int T) {
		long long flow = 0;
		for (int _ = 1; _ <= 2; ++_) {
            memset(vis, 0x00, sizeof(vis));
            assert(this -> flow(S, T, 1) == 1);
        }
		return flow = 2;
	}
} yzh;

signed main() {
    scanf("%d%d", &n, &m);
    for (int i = 1, u, v, w; i <= m; ++i) {
        scanf("%d%d%d", &u, &v, &w);
        add(u, v, w), add(v, u, w);
    }
    for (int i = 1; i <= n; ++i)
        if (!dfn[i])
            tarjan(i, 0);
    for (int i = 1; i <= scnt; ++i) {
        mx[i] = -0x3f3f3f3f;
        mi[i] = 0x3f3f3f3f;
    }
    for (int u = 1; u <= n; ++u)
        for (int i = head[u]; i; i = edge[i].nxt) {
            int v = edge[i].to;
            if (sccno[v] != sccno[u]) continue;
            int w = edge[i].len;
            // printf("====== %d\n", w);
            int x = sccno[u];
            if (w > mx[x])
                mx[x] = w, xp[x] = i;
            if (w < mi[x])
                mi[x] = w, ip[x] = i;
        }
    int mm = 0, idx = -1;
    for (int i = 1; i <= scnt; ++i)
        if (mx[i] - mi[i] > mm) {
            mm = mx[i] - mi[i];
            idx = i;
        }
    printf("%d\n", mm);
    int X = xp[idx], I = ip[idx];
    u1 = edge[X].to, v1 = edge[X ^ 1].to;
    u2 = edge[I].to, v2 = edge[I ^ 1].to;
    
    if (u1 == v2) swap(u2, v2), I ^= 1;
    if (v1 == u2) swap(u1, v1), X ^= 1;
    if (v1 == v2) swap(u2, v2), I ^= 1, swap(u1, v1), X ^= 1;
    
    fprintf(stderr, "(%d, %d): %d\n", u1, v1, edge[X].len);
    fprintf(stderr, "(%d, %d): %d\n", u2, v2, edge[I].len);
    
    iii = I, xxx = X;
    
    int III = n + 1, XXX = n + 2;
    
    yzh.init(n + 2);
    
    yzh.add(III, u1, 1);
    yzh.add(III, v1, 1);
    yzh.add(u2, XXX, 1);
    yzh.add(v2, XXX, 1);
    
    for (int u = 1; u <= n; ++u)
        if (sccno[u] == idx)
            for (int i = head[u]; i; i = edge[i].nxt) {
                int v = edge[i].to;
                if (sccno[v] != idx) continue;
                if (v > u) continue;
                if (u == u1 && v == v1) continue;
                if (u == v1 && v == u1) continue;
                if (u == u2 && v == v2) continue;
                if (u == v2 && v == u2) continue;
                // puts("dfsdfsdfsdfsdfs");
                // yzh.add(u, v, 1);
                yzh.add(v, u, 1, true);
            }
    
    int res = yzh.solve(III, XXX);
    
    fprintf(stderr, "maxflow = %d\n", res);
    
    assert(res == 2);
    
    int TTTTTTTT = 0;
    
    for (int u = u1; u != XXX; ) {
        ans.push_back(u);
        for (int i = yzh.xym.head[u]; i; i = yzh.xym.edge[i].nxt) {
            int v = yzh.xym.edge[i].to;
            if (yzh.xym.edge[i].flow != 1) continue;
            if (v == III) continue;
            if (v == XXX) {
                TTTTTTTT = u2 ^ v2 ^ u;
            }
            u = v;
            break;
        }
    }
    
    vector<int> tmp;
    for (int u = TTTTTTTT; u != III; ) {
        // printf("u = %d\n", u);
        tmp.push_back(u);
        for (int i = yzh.xym.head[u]; i; i = yzh.xym.edge[i].nxt) {
            int v = yzh.xym.edge[i].to;
            if (yzh.xym.edge[i ^ 1].flow != 1) continue;
            if (v == XXX) continue;
            u = v;
            break;
        }
    }
    // reverse(tmp.begin(), tmp.end());
    
    for (int x : tmp) ans.push_back(x);
    
    // intree[X] = intree[X ^ 1] = true;
    // intree[I] = intree[I ^ 1] = true;
    
    // dfs(v1, 0);
    
    // // for (int u = 1; u <= n; ++u)
    // //     printf(">>> %d %d\n", u, FA[u]);
    
    // queue<int> Q;
    // Q.push(v1), can[v1] = true;
    // while (!Q.empty()) {
    //     int u = Q.front(); Q.pop();
    //     for (int i = head[u]; i; i = edge[i].nxt) {
    //         if (!intree[i]) continue;
    //         int v = edge[i].to;
    //         if (v == FA[u]) continue;
    //         Q.push(v);
    //     }
    //     if (can[u]) {
    //         for (int i = head[u]; i; i = edge[i].nxt) {
    //             if (intree[i]) continue;
    //             int v = edge[i].to;
    //             if (can[v]) continue;
    //             can[v] = true;
    //             fff[v] = u;
    //         }
    //     }
    // }
    
    // // printf("DOWN = %d\n", DOWN);
    
    // bool rerrr = dfs(DOWN);
    
    // assert(rerrr);
    
    // for (int x = FA[DOWN]; FA[x]; x = FA[x])
    //     ans.emplace_back(x);
    
    // assert(can[DOWN]);
    
    printf("%d\n", int(ans.size()));
    for (int x : ans) printf("%d ", x);
    
    // vector<int> vec1, vec2;
    // ins[X] = ins[X ^ 1] = true;
    // ins[I] = ins[I ^ 1] = true;
    // bool res1 = dfs(u1, 0, vec1, u2);
    // assert(res1);
    // memset(vis, 0x00, sizeof(vis));
    // bool res2 = dfs(v2, 0, vec2, v1);
    // if (!res2) {
    //     swap(u2, v2);
    //     fprintf(stderr, "(%d, %d): %d\n", u1, v1, edge[X].len);
    //     fprintf(stderr, "(%d, %d): %d\n", u2, v2, edge[I].len);
    //     vec1.clear();
    //     memset(vis, 0x00, sizeof(vis));
    //     memset(ins, 0x00, sizeof(ins));
    //     ins[X] = ins[X ^ 1] = true;
    //     ins[I] = ins[I ^ 1] = true;
    //     res1 = dfs(u1, 0, vec1, u2);
    //     assert(res1);
    //     memset(vis, 0x00, sizeof(vis));
    //     res2 = dfs(v2, 0, vec2, v1);
    //     assert(res2);
    // }
    // printf("%d\n", int(vec1.size() + vec2.size()));
    // // assert(res2);
    // for (int x : vec2) printf("%d ", x);
    // for (int x : vec1) printf("%d ", x);
    // // puts("");
    // // reverse(vec2.begin(), vec2.end());
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5 7
1 2 1
1 3 -2
2 3 1
3 4 3
4 5 1
1 5 -1
2 5 2

output:

5
4
3 1 5 4 

result:

ok ok

Test #2:

score: 0
Accepted
time: 22ms
memory: 10696kb

input:

99997 100000
12238 99016 352755196
99016 25485 -412473602
25485 2440 991507552
2440 31171 -181894654
36970 2440 -800167579
2440 41865 -148191946
96629 31171 847888506
36970 95740 395546542
27992 2440 647886610
99016 29557 369124914
80795 27992 -673871966
36970 3509 573208857
57672 29557 874406776
41...

output:

1959330954
56
96048 4697 44442 68883 69259 57672 29557 99016 25485 73944 10503 93639 43423 7822 22393 79804 21467 41262 37318 80875 964 80256 57732 56899 24616 94522 90367 27992 2440 31171 95092 34883 46301 96778 37694 88289 30288 68523 54073 84997 89628 67966 84407 3463 72825 51491 87712 96230 2207...

result:

ok ok

Test #3:

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

input:

99997 100000
41884 21178 -431811360
41884 42699 -450057006
36523 21178 582079730
21178 96679 615552614
63637 21178 498974417
96679 5108 235820276
75058 41884 220112636
35148 42699 589595309
36523 18002 -637739861
65854 5108 -312755792
45137 41884 -511118771
5108 31311 554050951
25335 35148 -28341059...

output:

1968439328
40
98098 99686 71024 89860 18331 78274 35287 14678 59612 54901 26981 83047 87303 41126 28694 6204 50088 5108 7577 69337 51434 52252 25926 90144 26071 33264 70298 99716 98733 71151 55330 83919 83711 38202 85192 45137 41884 42699 87673 23692 

result:

ok ok

Test #4:

score: -100
Wrong Answer
time: 20ms
memory: 12204kb

input:

99984 99999
33974 29867 335681778
33974 87468 348956829
83048 87468 320849805
29867 69456 -424530698
72457 69456 -950650074
53838 83048 755969166
85914 69456 569454441
51728 87468 -202158773
15970 29867 -865071002
15970 94894 697607001
94894 74694 616318126
33974 11496 -89287579
53838 34365 -6577379...

output:

1985932414
95
80694 78849 28078 91542 83656 97838 70238 5449 75902 51728 87468 83048 53838 99503 39863 14477 96407 37891 95578 37766 93343 43068 834 53261 74033 70104 67164 18619 15809 27495 53416 39906 68004 89447 91521 47303 29675 2715 26901 15891 40356 94629 33513 14218 53663 48757 81135 48441 16...

result:

wrong answer Cycle contains repeated edge 53838-83048