/*
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. Maven+Eclipse+SparkStreaming+Kafka整合

    版本号: maven3.5.0     scala IDE for Eclipse:版本(4.6.1)    spark-2.1.1-bin-hadoop2.7    kafka_2.11-0.8.2 ...

  2. 基于select类型多路IO复用,实现简单socket并发

    还有很多缺限,如客户断开无限重复 以下转至老师博客: server: #!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = " ...

  3. 各种类型的Json格式化

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.R ...

  4. Complexity and Tractability (3.44) - The Traveling Salesman Problem

    Copied From:http://csfieldguide.org.nz/en/curriculum-guides/ncea/level-3/complexity-tractability-TSP ...

  5. JAVA常量与变量

    顺着箭头的转换为自动转换逆这箭头的转换为强制转换. 常量 关键字FINAL 命名为大写 标识符 1要以字母数字下划线和¥组成 2首字母不能为数字 3不能是JAVA的关键字和 保留字 4数据类型分为基本 ...

  6. python基本知识点

    1.基本数据类型 1.1int 字符串转换为数字,比如 a = “123” print(type(a) , a) b = int(a) print(type(b),b) num = “b” v = i ...

  7. 修改docker容器的端口映射

    大家都知道docker run可以指定端口映射,但是容器一旦生成,就没有一个命令可以直接修改.通常间接的办法是,保存镜像,再创建一个新的容器,在创建时指定新的端口映射. 有没有办法不保存镜像而直接修改 ...

  8. elixir环境配置

    mac下 brew install elixir debian下版本可能太低,需要kiex 安装 curl -sSL https://raw.githubusercontent.com/taylor/ ...

  9. git .gitignore文件

    .gitignore ! /*   忽略所有的文件 !/pages/  添加根目录下的所有文件被跟踪

  10. 【rabbitmq】rabbitmq概念解析--消息确认--示例程序

    概述 本示例程序全部来自rabbitmq官方示例程序,rabbitmq-demo: 官方共有6个demo,针对不同的语言(如 C#,Java,Spring-AMQP等),都有不同的示例程序: 本示例程 ...