你有一张无向带权图。每次加一条边,你可以选出当前所有边的一个子集,要求使得所有点度数为奇数。每次加边后问你所选边最大权的最小值是多少。
n , m ≤ 3 × 1 0 5 n,m\leq3\times10^5 n,m≤3×105
只要每个连通块大小都是偶数,那么一定有可行方案。
O ( m log n log m ) O(m\log n\log m) O(mlognlogm)
#include
using namespace std;
const int N = 3e5 + 10;
int n, m;
int sz[N], fa[N], cnt, rk[N];
int ans[N];
struct edge{
int x, y, w, no;
} e[N];
bool cmp(edge a, edge b) {return a.w < b.w;}
int fi;
vector<edge> b[N * 4];
multiset<int> em;
int gf(int x) {
return fa[x] == 0 ? x : gf(fa[x]);
}
struct state{
int x,y,w,is_rk_same;
};
stack<state> S;
void merge(edge e) {
int x = gf(e.x), y = gf(e.y);
if (rk[x] > rk[y]) swap(x,y);
if (x != y) {
em.insert(e.w);
state a = (state){x, y, e.w, rk[x] == rk[y]};
S.push(a);
fa[x] = y;
if (rk[y] == rk[x]) rk[y]++;
if ((sz[y] & 1) && (sz[x] & 1)) cnt -= 2;
sz[y] += sz[x];
} else S.push((state){0,0,0,0});
}
void pop() {
state a = S.top(); S.pop();
if (a.x == 0) return;
sz[a.y] -= sz[a.x];
fa[a.x] = 0;
if (a.is_rk_same) rk[a.y]--;
em.erase(em.find(a.w));
if ((sz[a.y] & 1) && (sz[a.x] & 1)) cnt += 2;
}
void modify(int x, int l, int r, int tl, int tr, const edge &a) {
if (l > tr || r < tl) return;
if (tl <= l && r <= tr) {
b[x].push_back(a); return;
}
modify(x << 1, l, l + r >> 1, tl, tr, a);
modify(x << 1 | 1, (l + r >> 1) + 1 , r, tl, tr, a);
}
void solve(int x, int l, int r) {
for(edge t : b[x])
merge(t);
if (l == r) {
int lafi = fi;
if (cnt != 0) {
int i = fi + 1;
for(; i <= m; i++) {
if (cnt == 0) break;
if (e[i].no <= l) {
merge(e[i]);
modify(1, 1, m, e[i].no, l - 1, e[i]);
}
}
fi = i - 1;
}
if (cnt == 0) ans[l] = *em.rbegin();
for(int i = fi; i > lafi; i--) if (e[i].no <= l) pop();
} else {
solve(x << 1 | 1, (l + r >> 1) + 1, r);
solve(x << 1, l, l + r >> 1);
}
for(int i = 0; i < b[x].size(); i++)
pop();
}
int main() {
freopen("e.in","r",stdin);
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int x, y, w;
scanf("%d %d %d",&x, &y, &w);
e[i] = (edge) {x, y, w, i};
}
sort(e + 1, e + 1 + m, cmp);
for(int i = 1; i <= n; i++) sz[i] = 1; cnt = n;
solve(1, 1, m);
for(int i = 1; i <= m; i++) if (ans[i] == 0) printf("%d\n",-1);
else printf("%d\n",ans[i]);
}