Break up CF700C

Break up

CF700C

首先考虑只能删一条边的做法,我们可以找出所有的桥,然后随便跑一条 S 到 T 路径,如果这条路径上有桥就说明可以,否则不行

发现这个做法其实是 O(M) 的

那么可以先随便找一条 N 到 M 的路径,分别尝试删这条路径上的边再套上面做法就好了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
//#define int long long
#define MAXN 3016
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define inf 0x3f3f3f3f
#define cmx( a , b ) a = max( a , b )
#define cmn( a , b ) a = min( a , b )
int n , m , s , t;
int head[MAXN] , nex[MAXN * 50] , to[MAXN * 50] , wto[MAXN * 50] , ecn = -1;
void ade( int u , int v , int w ) {
nex[++ecn] = head[u] , to[ecn] = v , wto[ecn] = w , head[u] = ecn;
}
int vis[MAXN] , pre[MAXN];
int fuck = 0x7f7f7f7f;
bool findpath( int u ) {
if( u == t ) return true;
vis[u] = 1;
for( int i = head[u] ; ~i ; i = nex[i] ) {
if( i == fuck || i == ( fuck ^ 1 ) ) continue;
int v = to[i];
if( vis[v] ) continue;
pre[v] = i ^ 1;
if( findpath( v ) ) return true;
}
return false;
}
int clo;
int dfn[MAXN] , low[MAXN];
int cut[MAXN * 50] , done[MAXN];
int use[MAXN * 50];
void tarjan( int u ) {
done[u] = 1;
dfn[u] = low[u] = ++ clo;
for( int i = head[u] ; ~i ; i = nex[i] ) {
if( use[i] ) continue;
use[i] = use[i ^ 1] = 1;
if( i == fuck || i == ( fuck ^ 1 ) ) continue;
int v = to[i];
if( !dfn[v] ) {
tarjan( v );
low[u] = min( low[u] , low[v] );
if( low[v] > dfn[u] ) cut[i] = cut[i ^ 1] = true;
} else if( done[v] == 1 )
low[u] = min( low[u] , dfn[v] );
}
done[u] = 2;
}
vector<int> eds;
pii ans;
int main() {
memset( head , -1 , sizeof head );
cin >> n >> m >> s >> t;
for( int i = 1 , u , v , w ; i <= m ; ++ i ) {
scanf("%d%d%d",&u,&v,&w);
ade( u , v , w ) , ade( v , u , w );
}
if( !findpath( s ) ) return puts("0") , puts("0") , 0;
int c = t;
while( c != s )
eds.pb( pre[c] ) , c = to[pre[c]];
int res = 0x7f7f7f7f;
for( int i = 0 ; i < eds.size() ; ++ i ) {
fuck = eds[i];
memset( cut , 0 , sizeof cut );
memset( done , 0 , sizeof done );
memset( use , 0 , sizeof use );
memset( dfn , 0 , sizeof dfn );
memset( low , 0 , sizeof low );
clo = 0;
tarjan( s );
memset( vis , 0 , sizeof vis );
if( ! findpath( s ) ) {
if( res > wto[fuck] )
res = wto[fuck] ,
ans = mp( 0 , fuck >> 1 );
continue;
}
c = t;
while( c != s ) {
if( cut[pre[c]] )
if( res > wto[fuck] + wto[pre[c]] )
res = wto[fuck] + wto[pre[c]] , ans = mp( fuck >> 1 , pre[c] >> 1 );
c = to[pre[c]];
}
}
if( res == 0x7f7f7f7f ) return puts("-1") , 0;
if( ! ans.fi ) {
printf("%d\n",wto[ans.se << 1]);
puts("1");
printf("%d",ans.se + 1);
} else {
printf("%d\n",res);
puts("2");
printf("%d %d" , ans.fi + 1 , ans.se + 1);
}
}
文章作者: yijan
文章链接: https://yijan.co/old36/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Yijan's Blog