/*
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. 基于SVN提交历史筛选作者并修改文件内容

    笔者最近开发的项目中,是通过SVN做为版本管理工具的,因为需要创建的文件太多,所以有许多文件是在原有文件基础上拷贝过来修改的,这里就涉及到一个问题,原有文件中注释里填的JAVA类名.作者工号.创建时间 ...

  2. AIX系统日志

    1.系统错误日志 存放路径:/var/adm/ras/errlog 说明:该日志记录了系统所检测到的软硬件故障和错误,尤其对系统的硬件故障有很大的参考价值,是AIX提供的最有价值的日志之一, errl ...

  3. npm 切换淘宝源

    由于Node官方模块仓库太慢,建议将模块仓库切换到阿里源 C:\workspace\angular>npm config set registry https://registry.npm.ta ...

  4. app崩溃后自动重启

    android 引用:http://blog.csdn.net/caiwenfeng_for_23/article/details/41184353 package com.tan.abnormalr ...

  5. Oracle和SQL语句的优化策略(基础篇)

    转载自: http://blog.csdn.net/houpengfei111/article/details/9245337 http://blog.csdn.net/uniqed/article/ ...

  6. php 函数func_get_args()、func_get_arg()与func_num_args()之间的比较

    在PHP的官方文档上的个自定义如下: func_get_args():返回一个包含函数参数列表的数组. func_get_arg():返回指定的参数值. func_num_args():返回调用函数的 ...

  7. codeblocks “can't find compiler executable in yourconfigured search ……”

    新安装的codeblocks 16.01,安装后打开提示如下,没法用..原因是编译器并没有找对自己安装的 mingw 的安装位置. 解决办法:如下图点击 Auto-detect 之后,会看到位置信息变 ...

  8. 黄聪:多个wordpress网站(不同域名)共享用户数据的方法

    WordPress可以自定义用户数据表,这样多个wordpress网站就可以共享用户数据了,有时候这是非常方便的,这些Wordpress站点应该安装在同一个数据库下,数据表前缀各不相同.由于Wordp ...

  9. 关于nginx大流量负载调优

    优化nginx包括两方面: 1.是自己重写nginx代码(比如tengine).本身nginx的代码已经足够优秀,如果不是每秒几千的请求,就忽略这个部分吧. 2.另一个就是和优化nginx的配置,这是 ...

  10. Shader-另类实现

    利用Panel的Clipping 裁剪模式(其实该实现方式也是shader)主要是修改 centX 与 Size就ok的 . panel.baseClipRegion=new Vector4(cenX ...