/*
LCT管子题(说的就是你 水管局长) 首先能得到一个结论, 那就是当且仅当所有联通块都是偶数时存在构造方案 LCT动态加边, 维护最小生成联通块, 用set维护可以删除的边, 假如现在删除后不影响全都是偶数大小的性质 就删除 不清楚link为啥要makeroot两次 */
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#include<set>
#include<cmath>
#define ll long long
#define M 400010
#define mmp make_pair
using namespace std;
int read() {
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
int n, m, cnt, ln[M], rn[M]; struct LCT {
int fa[M], ch[M][2], sz[M], rev[M], val[M], mx[M], pl[M], zz[M];
#define ls ch[x][0]
#define rs ch[x][1]
void pushup(int x) {
sz[x] = sz[ls] + sz[rs] + zz[x] + 1;
mx[x] = max(mx[ls], mx[rs]);
pl[x] = (mx[x] == mx[ls]) ? pl[ls]: pl[rs];
if(val[x] > mx[x]) mx[x] = val[x], pl[x] = x;
} bool isr(int x) {
return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
} void add(int x) {
rev[x] ^= 1;
swap(ls, rs);
} void pushdown(int x) {
if(rev[x]) {
add(ls), add(rs);
rev[x] = 0;
}
} void pd(int x) {
if(!isr(x)) pd(fa[x]);
pushdown(x);
} void rotate(int x) {
int y = fa[x], q = fa[y];
bool dy = (ch[y][1] == x), dz = (ch[q][1] == y);
if(!isr(y)) ch[q][dz] = x;
fa[x] = q;
fa[ch[x][dy ^ 1]] = y;
ch[y][dy] = ch[x][dy ^ 1];
ch[x][dy ^ 1] = y;
fa[y] = x;
pushup(y);
pushup(x);
} void splay(int x) {
pd(x);
while(!isr(x)) {
int y = fa[x];
if(!isr(y)) {
int q = fa[y];
if((ch[y][1] == x) ^ (ch[q][1] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
} void access(int x) {
for(int y = 0; x; y = x, x = fa[x]) splay(x), zz[x] += sz[rs] - sz[y], rs = y, pushup(x);
} void maker(int x) {
access(x);
splay(x);
add(x);
} int findr(int x) {
access(x);
splay(x);
while(ls) pushdown(x), x = ls;
return x;
} void link(int x, int y) {
maker(x);
maker(y);
fa[x] = y;
zz[y] += sz[x];
sz[y] += sz[x];
} void split(int x, int y) {
maker(x), access(y), splay(y);
} void cut(int x, int y) {
split(x, y);
if(fa[x] != y && ch[x][1]) return;
fa[x] = ch[y][0] = 0;
pushup(y);
} int query(int x, int y) {
split(x, y);
return pl[y];
} int sum(int x) {
maker(x);
return ((sz[x] + 1) >> 1) & 1;
} } lct;
set<pair<int, int> >st;
set<pair<int, int> >::reverse_iterator it;
int main() {
n = read(), m = read();
cnt = n;
// for(int i = 1; i <= n; i++) lct.sz[i] = 1;
for(int i = 1; i <= m; i++) {
int vi = read(), vj = read();
ln[i + n] = vi, rn[i + n] = vj;
int x = read();
lct.val[i + n] = x;
lct.pushup(i + n);
if(lct.findr(vi) != lct.findr(vj)) {
if(lct.sum(vi)) cnt--;
if(lct.sum(vj)) cnt--;
lct.link(vi, i + n);
lct.link(i + n, vj);
lct.split(vi, vj);
if(lct.sum(vj)) cnt++;
} else {
int pl = lct.query(vi, vj);
if(lct.val[pl] <= x) {
if(cnt == 0) printf("%d\n", st.rbegin()->first);
else puts("-1");
continue;
}
lct.cut(ln[pl], pl);
lct.cut(rn[pl], pl);
lct.link(vi, i + n);
lct.link(i + n, vj);
// lct.split(vi, vj);
st.erase(st.find(mmp(lct.val[pl], pl)));
}
st.insert(mmp(x, i + n));
if(cnt) {
puts("-1");
continue;
}
while(1) {
it = st.rbegin();
int id = it->second;
lct.cut(ln[id], id);
lct.cut(id, rn[id]);
if(lct.sum(ln[id]) || lct.sum(rn[id])) {
lct.link(ln[id], id);
lct.link(id, rn[id]);
break;
}
st.erase(*it);
}
printf("%d\n", st.rbegin()->first);
}
return 0;
}

CF603EPastoral Oddities的更多相关文章

  1. CF603E Pastoral Oddities

    CF603E Pastoral Oddities 度数不好处理.转化题意:不存在连通块为奇数时候就成功了(自底向上调整法证明) 暴力:从小到大排序加入.并查集维护.全局变量记录奇数连通块的个数 答案单 ...

  2. 【CF603E】Pastoral Oddities cdq分治+并查集

    [CF603E]Pastoral Oddities 题意:有n个点,依次加入m条边权为$l_i$的无向边,每次加入后询问:当前图是否存在一个生成子图,满足所有点的度数都是奇数.如果有,输出这个生成子图 ...

  3. Codeforces603E - Pastoral Oddities

    Portal Description 初始时有\(n(n\leq10^5)\)个孤立的点,依次向图中加入\(m(m\leq3\times10^5)\)条带权无向边.使得图中每个点的度数均为奇数的边集是 ...

  4. Codeforces 603E Pastoral Oddities

    传送门:http://codeforces.com/problemset/problem/603/E [题目大意] 给出$n$个点,$m$个操作,每个操作加入一条$(u, v)$长度为$l$的边. 对 ...

  5. CF603E Pastoral Oddities 优先队列+结论+LCT维护生成树

    首先,一个神奇的结论:一个合法的方案存在的条件是每一个联通块的节点数都是偶数个的. 这个可以用数学归纳法简单证一证. 证出这个后,我们只需动态加入每一个边,并查看一下有哪些边能够被删除(删掉后联通块依 ...

  6. On having layout

    英文原文在此:http://www.satzansatz.de/cssd/onhavinglayout.htm 介绍 Internet Explorer 中有很多奇怪的渲染问题可以通过赋予其“layo ...

  7. cf Round 603

    A.Alternative Thinking(思维) 给出一个01串,你可以取反其中一个连续子串,问取反后的01子串的最长非连续010101串的长度是多少. 我们随便翻一个连续子串,显然翻完之后,对于 ...

  8. CBO学习----03--选择率(Selectivity)

    第3章 单表选择率(Single Table Selectivity) Selectivity是优化器估算Rows(Cards)的重要依据. /**************************** ...

  9. Content related to smartcards (and RFID/NFC)

    Introduction Add your content here. ISO/IEC 7816 Contact Cards Hardware EMV payment cards Orange Cas ...

随机推荐

  1. direct3d

    DirectX for .Net procedure 1, install DXSDK  https://www.microsoft.com/en-us/download/details.aspx?i ...

  2. Bjarne Stroustrup announces C++ Core Guidelines

    This morning in his opening keynote at CppCon, Bjarne Stroustrup announced the C++ Core Guidelines ( ...

  3. bzoj 3600 没有人的算术——二叉查找树动态标号

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3600 已知 l 和 r 的排名,想快速知道 k 的排名.那么建一个 BIT ,用已知的排名做 ...

  4. str.replace()和re.sub()/calendar.month_abbr/re.subn()/upper和lower和capitalize/贪婪匹配和费贪婪匹配/re.S和re.DOTALL 笔记

    str.replace()可以进行简单的替换 >>> a = 'one.txt, index.py, index.php, index.html, index.js' >> ...

  5. ASP.NET AJAX入门系列(3):使用ScriptManagerProxy控件

    在ASP.NET AJAX中,由于一个ASPX页面上只能有一个ScriptManager控件,所以在有母版页的情况下,如果需要在Master-Page和Content-Page中需要引入不同的脚本时, ...

  6. python json.dumps(output) ^ SyntaxError: invalid syntax

    问题 下面代码在有些机器上执行正常,有些机器上执行报错: import json output={} print json.dumps(output) python代码报错: line 277 pri ...

  7. RedHat7安装vmware虚拟机启动报错

    提示错误如下: Kernel Headers for version 3.10.0-229.14.1.el7.x86_64 were not found.If you.... 安装kernel dev ...

  8. Azure ARM (18) 将Azure RM Manage Disk托管磁盘的Image,跨订阅迁移

    <Windows Azure Platform 系列文章目录> 先挖一个坑,以后再埋. 最近遇到一个客户需求,客户使用了Azure RM Manage Disk托管磁盘,然后捕获镜像做成了 ...

  9. PAT 乙级 1079 延迟的回文数(20 分)

    1079 延迟的回文数(20 分) 给定一个 k+1 位的正整数 N,写成 a​k​​⋯a​1​​a​0​​ 的形式,其中对所有 i 有 0≤a​i​​<10 且 a​k​​>0.N 被称 ...

  10. Service Mesh简介

    1.1 Service Mesh   1.1.1 什么是Service Mesh Service Mesh是最近才兴起的一个名词,最早在2016年9月29日由开发Linkerd的Buoyant公司首次 ...