Solution -「LOCAL」割海成路之日
\(\mathcal{Description}\)
给定 \(n\) 个点的一棵树,有 \(1,2,3\) 三种边权。一条简单有向路径 \((s,t)\) 合法,当且仅当走过一条权为 \(3\) 的边之后,只通过了权为 \(1\) 的边。\(m\) 次询问,每次询问给定 \(a,b,s,t\),表示将边 \((a,b)\) 的权 \(-1\)(若权已为 \(1\) 则不变),并询问 \(t\) 是否能走到 \(s\);有多少点能够走到 \(s\)。
\(n,m\le 3 \times 10^5\)。
\(\mathcal{Solution}\)
由于是求多少点可达 \(s\),考虑把路径的规则反过来:一开始只能走权为 \(1\) 的边,放一次“大招”(走过权为 \(3\) 的边)后就能任意走,但只能开一次大。问题变成求 \(s\) 是否可达 \(t\),\(s\) 可达多少点。
显然,\(s\) 可达的点可以归为如下几类:
与 \(s\) 在同一个 \(1-\)联通块。
处于一个 \(12-\)联通块,且该联通块由权为 \(3\) 的边与 \(s\) 所在的 \(1-\)联通块相连。
考虑用并查集维护 \(1-\)联通块和 \(12-\)联通块。第一类点直接求 size 就可以了。第二类点,用每一个 \(12-\)联通块的根向父亲贡献,最后加上父亲对当前块的贡献即为答案。
\(\mathcal{Code}\)
/* Clearink */
#include <cstdio>
inline int rint () {
int x = 0, f = 1; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () ) f = s == '-' ? -f : f;
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
return x * f;
}
template<typename Tp>
inline void wint ( Tp x ) {
if ( x < 0 ) putchar ( '-' ), x = ~ x + 1;
if ( 9 < x ) wint ( x / 10 );
putchar ( x % 10 ^ '0' );
}
const int MAXN = 3e5;
int n, m, ecnt = 1, head[MAXN + 5], fa[MAXN + 5], facol[MAXN + 5], rchs[MAXN + 5];
struct Edge { int to, cst, nxt; } graph[MAXN * 2 + 5];
inline void link ( const int s, const int t, const int c ) {
graph[++ ecnt] = { t, c, head[s] };
head[s] = ecnt;
}
struct DSU {
int fa[MAXN + 5], siz[MAXN + 5];
inline int operator () ( const int k ) { return find ( k ); }
inline int operator [] ( const int k ) const { return siz[k]; }
inline void init () {
for ( int i = 1; i <= n; ++ i ) {
fa[i] = i, siz[i] = 1;
}
}
inline int find ( const int x ) { return x ^ fa[x] ? fa[x] = find ( fa[x] ) : x; }
inline bool unite ( int x, int y ) {
if ( ( x = find ( x ) ) == ( y = find ( y ) ) ) return false;
return siz[fa[x] = y] += siz[x], true;
}
} dsu[2];
inline void init ( const int u ) {
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ( v = graph[i].to ) ^ fa[u] ) {
fa[v] = u, facol[v] = graph[i].cst;
init ( v );
}
}
}
int main () {
freopen ( "sea.in", "r", stdin );
freopen ( "sea.out", "w", stdout );
n = rint (), m = rint ();
for ( int i = 1, u, v, c; i < n; ++ i ) {
u = rint (), v = rint (), c = rint ();
link ( u, v, c ), link ( v, u, c );
}
dsu[0].init (), dsu[1].init (), init ( 1 );
for ( int i = 2; i <= n; ++ i ) {
if ( facol[i] <= 2 ) dsu[1].unite ( i, fa[i] );
if ( facol[i] <= 1 ) dsu[0].unite ( i, fa[i] );
}
for ( int i = 2; i <= n; ++ i ) {
if ( facol[i] == 3 ) {
rchs[dsu[0]( fa[i] )] += dsu[1][i];
}
}
for ( int a, b, s, t; m --; ) {
a = rint (), b = rint (), s = rint (), t = rint ();
if ( fa[a] == b ) a ^= b ^= a ^= b;
if ( facol[b] == 3 ) {
-- facol[b];
rchs[dsu[0]( a )] -= dsu[1][b];
rchs[dsu[0]( fa[dsu[1]( a )] )] += dsu[1][b];
dsu[1].unite ( b, a );
} else if ( facol[b] == 2 ) {
-- facol[b];
rchs[dsu[0]( a )] += rchs[b];
dsu[0].unite ( b, a );
}
putchar ( dsu[1]( s ) == dsu[1]( t )
|| dsu[0]( fa[dsu[1]( t )] ) == dsu[0]( s )
|| dsu[1]( fa[dsu[0]( s )] ) == dsu[1]( t ) ?
'1' : '0' ), putchar ( ' ' );
wint ( dsu[1][dsu[1]( s )] + rchs[dsu[0]( s )]
+ ( facol[dsu[0]( s )] == 3 ? dsu[1][dsu[1]( fa[dsu[0]( s )] )] : 0 ) );
putchar ( '\n' );
}
return 0;
}
\(\mathcal{Details}\)
考试的时候拿阳寿去肝 T2,压根没发现这题水得多 qwqwq。
Solution -「LOCAL」割海成路之日的更多相关文章
- lfyzoj103 割海成路之日
问题描述 现在,摆在早苗面前的是一道简单题.只要解决了这道简单题,早苗就可以发动她现人神的能力了: 输出 \[1\ \mathrm{xor}\ 2\ \mathrm{xor} \cdots \math ...
- NOIP 模拟 $28\; \rm 割海成路之日$
题解 \(by\;zj\varphi\) 用两个集合分别表示 \(1\) 边联通块,\(1,2\) 边联通块 . \(\rm son_x\) 表示当前节点通过 \(3\) 类边能到的 \(2\) 联通 ...
- Solution -「LOCAL」大括号树
\(\mathcal{Description}\) OurTeam & OurOJ. 给定一棵 \(n\) 个顶点的树,每个顶点标有字符 ( 或 ).将从 \(u\) 到 \(v\) ...
- Solution -「LOCAL」Drainage System
\(\mathcal{Description}\) 合并果子,初始果子的权值在 \(1\sim n\) 之间,权值为 \(i\) 的有 \(a_i\) 个.每次可以挑 \(x\in[L,R]\) ...
- Solution -「LOCAL」Burning Flowers
灼之花好评,条条生日快乐(假装现在 8.15)! \(\mathcal{Description}\) 给定一棵以 \(1\) 为根的树,第 \(i\) 个结点有颜色 \(c_i\) 和光亮值 ...
- Solution -「LOCAL」舟游
\(\mathcal{Description}\) \(n\) 中卡牌,每种三张.对于一次 \(m\) 连抽,前 \(m-1\) 次抽到第 \(i\) 种的概率是 \(p_i\),第 \(m\) ...
- Solution -「LOCAL」解析电车
\(\mathcal{Description}\) 给定 \(n\) 个点 \(m\) 条边的无向图,每条边形如 \((u,v,r)\),表示 \(u,v\) 之间有一条阻值为 \(r\Omega ...
- Solution -「LOCAL」二进制的世界
\(\mathcal{Description}\) OurOJ. 给定序列 \(\{a_n\}\) 和一个二元运算 \(\operatorname{op}\in\{\operatorname{ ...
- Solution -「LOCAL」过河
\(\mathcal{Description}\) 一段坐标轴 \([0,L]\),从 \(0\) 出发,每次可以 \(+a\) 或 \(-b\),但不能越出 \([0,L]\).求可达的整点数. ...
随机推荐
- 第10组 Beta冲刺 (2/5)
1.1基本情况 ·队名:今晚不睡觉 ·组长博客:https://www.cnblogs.com/cpandbb/p/14015412.html ·作业博客:https://edu.cnblogs.co ...
- Golang中Label的用法
在Golang中能使用Label的有goto, break, continue.,这篇文章就介绍下Golang中Label使用和注意点. 注意点: Label在continue, break中是可选的 ...
- Linux上天之路(八)之用户和组
主要内容. 用户创建,删除,修改 密码及密码文件 组创建,删除,修改 组密码及组配置文件 相关文件 Linux用户分类 超级管理员: UID为0 root用户拥有至高无上的命令,root用户不能改名 ...
- Leetcode系列之两数之和
Leetcode系列之两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你 ...
- 通过js触发onPageView和event事件获取页面信息
注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6814814715022148100/ 承接上一篇文档<js页面触发launch事件编写> pageVi ...
- 《设计模式面试小炒》策略和工厂模式替代业务场景中复杂的ifelse
<设计模式面试小炒>策略和工厂模式替代业务场景中复杂的ifelse 我是肥哥,一名不专业的面试官! 我是囧囧,一名积极找工作的小菜鸟! 囧囧表示:小白面试最怕的就是面试官问的知识点太笼统, ...
- Ultimaker2+使用指南
USB打印 使用最新的官方软件即可进行USB3D打印. 使用时在选项上进行预热以及x,y,z重新归零定位,每次开始都要做. SD卡打印 比较方便,因为电脑可能拿去做其他事情. 打印机堵塞 退出耗材时候 ...
- Qt中编译器
很多时候,Qt构建项目编译的过程中会报错,大部分报错是因为qt的设置出现问题,很多时候环境配置时要选择合适的编译器,debugger调试器等,这里对一些名词解释,内容对新手很友好,大佬就不用看啦. M ...
- iptables规则管理
查看规则 iptables -t filter -L INPUT -n -v --line 省略-t选项时,表示默认操作filter表中的规则 添加规则 注意点:添加规则时,规则的顺序非常重要 - ...
- 【程序5】输入三个整数x,y,z,请把这三个数由小到大输出
我自己写的: x = int(input('x:')) y = int(input('y:')) z = int(input('z:')) L = [x,y,z] print(sorted(L)) 官 ...