/*
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. SmartGit过期解决办法

    1. 快捷键win+R ,输入:%APPDATA%\syntevo\SmartGit\2. 进入smart的版本 如18.1 然后删除setting.xml文件

  2. Maven install报MojoFailureException

    [ERROR] 位置: 类 com.spark.test.JavaDirectKafkaWordCount [ERROR] /I:/TrueTimeControlOnSparkByJava/src/m ...

  3. nginx支持android、ios、微信扫一扫

    首先做一个android下载的html页面,页面中识别微信浏览器提示在浏览器中打开,然后在nginx对ios进行识别并跳转到apple store #下载App location ^~ /appDow ...

  4. delphi中Time消息的使用方法

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  5. java流程控制与选择控制

    流程控制语句 顺序   程序的正常执行 选择 if else多重if,嵌套if,switch; 循环 for whlie,do whlie; 案例1 自己对代码进行改进!!!!!!!!!!!!!!! ...

  6. 黄聪:如何使用钩子定制WordPress添加媒体界面,去除不需要的元素

    原文:http://www.solagirl.net/customize-wordpress-media-upload-ui.html WordPress编写文章界面的添加媒体按钮允许用户上传多媒体文 ...

  7. Xshell5 评估过期,需要采购,不能使用

    Xshell5 评估过期,需要采购,不能使用 标签: Xshell linux 2017年10月10日 13:13:1029507人阅读 评论(9) 收藏 举报 版权声明:本文为博主原创文章,未经博主 ...

  8. IDEA创建Springmvc项目

    项目主要步骤如下: 1.创建一个javaweb动态项目 2.导入springmvc demo所需要的jar包 3.生成项目war包 4.配置项目tomacat服务器 5.配置web.xml文件 6.编 ...

  9. C/C++基础----随机数分布和随机数引擎

    随机数分布 除了伯努利分布,其他都是模板,接收单个类型参数,指出分布生成的结果类型. 表示分布生成浮点数,float.double或long double 表示要求一个整型类型,不包括bool或任何c ...

  10. Bitmap BitmapData

    var sp:Sprite=new Sprite(); sp.graphics.beginFill(0xffccdd); sp.graphics.drawRect(0,0,100,100); sp.g ...