题目

Description


Input

Output

Sample Input
样例输入 1:
4 4
1 3 4
2 4 8
1 2 2
3 4 3
样例输入 2:
3 2
1 2 3
2 3 4

Sample Output
样例输出 1:
-1
8
8
3
样例输出 2:
-1
-1

Data Constraint

思路

首先,答案是单调不增的

并且容易证出,所有联通块大小为偶数

对于只回答最后一组询问,把边权排序,直到没有大小是技术的联通块就行

现在要推广到所有询问

考虑将时间看为x,边权看为y,这么看一条边就是平面上一个点。
将询问时间看为x,答案看为y,询问也变成了一个点。

我们就可以用分治乱搞。

代码

#include<bits/stdc++.h>
using namespace std;
const int N=3e5+77;
int n,m;
int sz[N],fa[N],cnt,rk[N];
int ans[N];
struct E
{int x,y,w,id;
}e[N];
bool cmp(E a,E b){return a.w<b.w;}
int fi;
vector<E> b[N*4];
multiset<int> em;
int gf(int x)
{return fa[x]==0?x:gf(fa[x]);
}
struct yjy
{int x,y,w,irs;
};
stack<yjy> S;
void ins(int x,int l,int r,int tl,int tr,const E &a)
{if(l>tr||r<tl) return;if(tl<=l&&r<=tr){b[x].push_back(a); return;}ins(x<<1,l,l+r>>1,tl,tr,a);ins(x<<1|1,(l+r>>1)+1,r,tl,tr,a);
}
void merge(E 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);yjy a=(yjy){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((yjy){0,0,0,0});
}
void pop()
{yjy a=S.top();S.pop();if(a.x==0)return;sz[a.y]-=sz[a.x];fa[a.x]=0;if(a.irs) rk[a.y]--;em.erase(em.find(a.w));if((sz[a.y]&1)&&(sz[a.x]&1)) cnt+=2;
}
void work(int x,int l,int r)
{for(E t:b[x]) merge(t);if(l==r){int LF=fi;if(cnt!=0){int i=fi+1;for(;i<=m;i++){if(cnt==0)break;if(e[i].id<=l){merge(e[i]);ins(1,1,m,e[i].id,l-1,e[i]);}}fi=i-1;}if(cnt==0)ans[l]=*em.rbegin();for(int i=fi; i>LF; i--)if(e[i].id<=l)pop();}else{work(x<<1|1,(l+r>>1)+1,r);work(x<<1,l,l+r>>1);}for(int i=0; i<b[x].size(); i++)pop();
}
int main()
{freopen("road.in","r",stdin);freopen("road.out","w",stdout);cin>>n>>m;for(int i=1,x,y,w; i<=m; i++){scanf("%d%d%d",&x,&y,&w);e[i]=(E){x,y,w,i};}sort(e+1,e+1+m,cmp);for(int i=1; i<=n; i++) sz[i]=1;cnt=n;work(1,1,m);for(int i=1; i<=m; i++) if(ans[i]==0) printf("%d\n",-1);else printf("%d\n",ans[i]);
}

【JZOJ 省选模拟】铺路 (road )相关推荐

  1. 【JZOJ 省选模拟】向日葵人生

    Description Input Output Sample Input 样例 1 输入: 2 3 2 1 2 2 3 Sample Output 样例 1 输出: 665496241 Data C ...

  2. 6687. 【JZOJ 省选模拟】树没了(tree)

    题目 Description Input Output 2操作的个数行,每行代表一个询问的答案. Sample Input 5 5 1 1 2 3 4 5 1 2 2 3 2 4 1 5 Q 1 M ...

  3. 【JZOJ 省选模拟】多项式(poly)

    题目 Description Input Output Sample Input 样例输入 1: 2 7 -7 14 样例输入 2: 3 1 4 1 5 样例输入 3: 0 998244353 Sam ...

  4. 【JZOJ 省选模拟】我的朋友们

    题目 Description Input Output 一行一个整数,表示题目描述中的期望对998244353取模的值. Sample Input 样例 1输入: 4 3 1 5 4 6 3 7 1 ...

  5. 【JZOJ 省选模拟】感受清风

    题目 Description Input Output 对于每个 qu 事件或者 qd 事件输出一个整数表示答案,答案对1000001取模(提示:这个数不 是质数,有101×9901=1000001) ...

  6. 【JZOJ 省选模拟】6681.图

    题目 Description 给定一张无重边.自环.割点的平面图,你需要回答 Q 次询问,每次询问会给出一个简单环,你需要回答在由这个简单环围成的多边形内部(包括边界上)的点有多少个. 保证图中每条线 ...

  7. 【JZOJ 省选模拟】6707.异或树(xor)

    题目 Description Input Output Sample Input 样例输入1: 2 2 998244353 样例输入2: 10 3 998244353 Sample Output 样例 ...

  8. 【JZOJ 省选模拟】人生(life)

    题目 Description Input 第一行一个正整数n. 第二行n个整数,其中第i个整数为1表示点i的颜色为黑色,为0表示点i的颜色为白色,为-1 则不确定. Output 输出一行一个整数表示 ...

  9. 【JZOJ 省选模拟】迷宫(maze)

    题目 Description Input Output Sample Input 样例输入 4 8 0 A 1 B 1 A 0 B 2 A 3 B 3 A 2 B 0 C 2 D 2 C 0 D 1 ...

最新文章

  1. java 抛异常给上级_java异常处理机制(示例代码)
  2. vscode 插件设置
  3. GraphPad Prism 教程,如何在坐标上放置2条生存曲线
  4. matplotlib绘图_使用matplotlib库绘图
  5. macOS 10.11.* 安装scrapy
  6. [Objective-c 基础 - 2.5] .h和.m文件,点语法,成员变量作用域
  7. datagridview输入数据格式化_第五节,输入输出函数
  8. 深度学习自学(三十):基于深度学习误排序学习的行人重识别对抗攻击
  9. 全才出书,值得一读——Leo推荐《我也能做CTO之程序员职业规划》
  10. SQL Serve——版本
  11. gettype php,gettype
  12. virtualxposed使用教程_VirtualXposed框架
  13. 5.1.3 NoSQL数据库-Redis(键值key-value)-五大数据类型
  14. Leetcode--Java--212. 单词搜索 II
  15. 微信中那些你不知道的隐藏实用功能!
  16. Angular 2 升级到 Angular 5
  17. Unity3D自制横版闯关游戏手册(1)
  18. snes9x 源码_仅64kb的SNES游戏如何制作优美的音乐
  19. ZZNUOJ_C语言1088:手机短号 (多实例)(完整代码)
  20. BC渗透的常见切入点(总结)

热门文章

  1. JavaSE 15 IDEA美化
  2. 渝首家跨国“威客”登陆美国
  3. 数字人民币概论、特征、架构介绍
  4. 工控服务器什么作用,工控机和服务器有什么区别
  5. 如何用Qt抠一个圆形头像出来
  6. JAVA公路车几何图_争当老司机 深入解读自行车几何图表
  7. 什么是以太网,它在WiFi世界中的价值是什么?
  8. 程序员一定会有35岁危机吗?三分天注定,七分靠打拼
  9. SMM项目登录注册简单实现
  10. Linux内核中断系统