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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "vector" #include "queue" #include "cmath" using namespace std; #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define MAXN 50006
int cn = 2; const int inf = 0x3f3f3f3f; class maxFlow { public: typedef long long ll; std::queue<int> q; std::vector<int> head, cur, nxt, to, dep; std::vector<ll> cap;
maxFlow(int _n = 0) { init(_n); } void init(int _n) { head.clear(); head.resize(_n + 1, 0); nxt.resize(2); to.resize(2); cap.resize(2); } void init() { init(head.size() - 1); } int add(int u, int v, ll w) { nxt.push_back(head[u]); int x = ( head[u] = to.size() ); to.push_back(v); cap.push_back(w); return x; } int Add(int u, int v, ll w) {
add(u, v, w); return add(v, u, 0); } void del(int x) { cap[x << 1] = cap[x << 1 | 1] = 0; } bool bfs(int s, int t, int delta) { dep.clear(); dep.resize(head.size(), -1); dep[s] = 0; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; i; i = nxt[i]) { int v = to[i]; ll w = cap[i]; if (w >= delta && dep[v] == -1) { dep[v] = dep[u] + 1; q.push(v); } } } return ~dep[t]; } ll dfs(int u, ll flow, int t, int delta) { if (dep[u] == dep[t]) return u == t ? flow : 0; ll out = 0; for (int& i = cur[u]; i; i = nxt[i]) { int v = to[i]; ll w = cap[i]; if (w >= delta && dep[v] == dep[u] + 1) { ll f = dfs(v, std::min(w, flow - out), t, delta); cap[i] -= f; cap[i ^ 1] += f; out += f; if (out == flow) return out; } } return out; } ll maxflow(int s, int t) { ll out = 0; ll maxcap = *max_element(cap.begin(), cap.end()); for (ll delta = 1ll << int(log2(maxcap) + 1e-12); delta; delta >>= 1) { while (bfs(s, t, delta)) { cur = head; out += dfs(s, 0x7fffffffffffffffll, t, delta); } } return out; } ll getflow(int x) const { return cap[x << 1 | 1]; } int vis[3000000]; void work( int u ) { vis[u] = 1; for( int i = head[u] ; i ; i = nxt[i] ) if( cap[i] ) { int v = to[i]; if( !vis[v] ) work( v ); } } } F ;
int n , m; vector<int> G[MAXN]; int g[MAXN][16] , p[MAXN][16] , dep[MAXN]; int T[MAXN] , S[MAXN] , tt[MAXN] , bacs[3000000] , bact[3000000]; void dfs( int u , int fa ) { for( int v : G[u] ) if( v != fa ) { dep[v] = dep[u] + 1; g[v][0] = u , p[v][0] = ++ cn; T[v] = F.Add( p[v][0] , 2 , 1 ); for( int k = 1 ; k < 16 ; ++ k ) if( g[g[v][k-1]][k-1] ) { g[v][k] = g[g[v][k-1]][k-1]; p[v][k] = ++ cn; F.Add( p[v][k] , p[v][k - 1] , inf ); F.Add( p[v][k] , p[g[v][k-1]][k-1] , inf ); } else break; dfs( v , u ); } } void link( int i , int u , int v ) { if( dep[u] < dep[v] ) swap( u , v ); for( int k = 15 ; k >= 0 ; -- k ) if( dep[g[u][k]] >= dep[v] ) F.Add( i , p[u][k] , inf ) , u = g[u][k]; if( u == v ) return; for( int k = 15 ; k >= 0 ; -- k ) if( g[u][k] != g[v][k] ) F.Add( i , p[u][k] , inf ) , F.Add( i , p[v][k] , inf ) , u = g[u][k] , v = g[v][k]; F.Add( i , p[u][0] , inf ) , F.Add( i , p[v][0] , inf ); } vector<int> s , t; pii E[MAXN]; int main() { cin >> n >> m; F.init( 3000000 ); for( int i = 1 , u , v ; i < n ; ++ i ) { scanf("%d%d",&u,&v); G[u].push_back( v ) , G[v].push_back( u ); E[i] = mp( u , v ); } dep[1] = 1 , dfs( 1 , 1 ); for( int i = 1 ; i < n ; ++ i ) if( g[E[i].fi][0] == E[i].se ) tt[i] = T[E[i].fi]; else tt[i] = T[E[i].se]; for( int i = 1 , u , v ; i <= m ; ++ i ) { scanf("%d%d",&u,&v); S[i] = F.Add( 1 , ++ cn , 1 ); link( cn , u , v ); } cout << F.maxflow( 1 , 2 ) << endl; for( int i = 1 ; i <= m ; ++ i ) bacs[S[i]] = i; for( int i = 1 ; i < n ; ++ i ) bact[tt[i]] = i; F.work( 1 ); for( int i = F.head[1] ; i ; i = F.nxt[i] ) if( !F.vis[F.to[i]] ) s.push_back( bacs[i ^ 1] );
for( int i = F.head[2] ; i ; i = F.nxt[i] ) if( F.vis[2] ^ F.vis[F.to[i]] ) t.push_back( bact[i] ); cout << s.size() << ' '; for( auto i : s ) printf("%d ",i); puts(""); cout << t.size() << ' '; for( auto i : t ) printf("%d ",i); puts(""); }
|