\(\mathcal{Description}\)

  OurOJ.

  给定 \(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」割海成路之日的更多相关文章

  1. lfyzoj103 割海成路之日

    问题描述 现在,摆在早苗面前的是一道简单题.只要解决了这道简单题,早苗就可以发动她现人神的能力了: 输出 \[1\ \mathrm{xor}\ 2\ \mathrm{xor} \cdots \math ...

  2. NOIP 模拟 $28\; \rm 割海成路之日$

    题解 \(by\;zj\varphi\) 用两个集合分别表示 \(1\) 边联通块,\(1,2\) 边联通块 . \(\rm son_x\) 表示当前节点通过 \(3\) 类边能到的 \(2\) 联通 ...

  3. Solution -「LOCAL」大括号树

    \(\mathcal{Description}\)   OurTeam & OurOJ.   给定一棵 \(n\) 个顶点的树,每个顶点标有字符 ( 或 ).将从 \(u\) 到 \(v\) ...

  4. Solution -「LOCAL」Drainage System

    \(\mathcal{Description}\)   合并果子,初始果子的权值在 \(1\sim n\) 之间,权值为 \(i\) 的有 \(a_i\) 个.每次可以挑 \(x\in[L,R]\) ...

  5. Solution -「LOCAL」Burning Flowers

      灼之花好评,条条生日快乐(假装现在 8.15)! \(\mathcal{Description}\)   给定一棵以 \(1\) 为根的树,第 \(i\) 个结点有颜色 \(c_i\) 和光亮值 ...

  6. Solution -「LOCAL」舟游

    \(\mathcal{Description}\)   \(n\) 中卡牌,每种三张.对于一次 \(m\) 连抽,前 \(m-1\) 次抽到第 \(i\) 种的概率是 \(p_i\),第 \(m\) ...

  7. Solution -「LOCAL」解析电车

    \(\mathcal{Description}\)   给定 \(n\) 个点 \(m\) 条边的无向图,每条边形如 \((u,v,r)\),表示 \(u,v\) 之间有一条阻值为 \(r\Omega ...

  8. Solution -「LOCAL」二进制的世界

    \(\mathcal{Description}\)   OurOJ.   给定序列 \(\{a_n\}\) 和一个二元运算 \(\operatorname{op}\in\{\operatorname{ ...

  9. Solution -「LOCAL」过河

    \(\mathcal{Description}\)   一段坐标轴 \([0,L]\),从 \(0\) 出发,每次可以 \(+a\) 或 \(-b\),但不能越出 \([0,L]\).求可达的整点数. ...

随机推荐

  1. Hive实现网站PV分析

    原文链接: https://www.toutiao.com/i6773241257528394248/ 之前我们做过<java mapreduce实现网站PV分析>,这次我们可以用hive ...

  2. Calendar日期往后一天,一月等

    import java.util.Date ;       Date date=new   Date();//取时间 System.out.println(date.toString());     ...

  3. Backbone.js 0.9.2 源码分析收藏

    Backbone 为复杂Javascript应用程序提供模型(models).集合(collections).视图(views)的结构.其中模型用于绑定键值数据和自定义事件:集合附有可枚举函数的丰富A ...

  4. 记一次 .NET 某消防物联网 后台服务 内存泄漏分析

    一:背景 1. 讲故事 去年十月份有位朋友从微信找到我,说他的程序内存要炸掉了...截图如下: 时间有点久,图片都被清理了,不过有点讽刺的是,自己的程序本身就是做监控的,结果自己出了问题,太尴尬了 二 ...

  5. RHCSA 第一天

    1.修改主机名: 查看主机名 2.查看日期 使用指定格式输出日期 YY-mm-DD HH:MM:SS 3.将"We are learning RHCSA"输出在屏幕上 4.使用ti ...

  6. 【刷题-LeetCode】200 Number of Islands

    Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...

  7. 微信小程序云开发框架

    概述 一直做后端服务器开发,最近看了一篇文章介绍小程序的云开发模式,觉得挺有意思,就尝试了一下,由本文做个记录. 因为不是专业的小程序开发人员,也没有做过网页开发,所以论述中出现错误难以避免,请多谅解 ...

  8. C# Reflection反射机制

    一.反射 什么是反射 .Net的应用程序由几个部分:'程序集(Assembly)'.'模块(Module)'.'类型(class)'组成: 反射提供一种编程的方式,让程序员可以在程序运行期获得这几个组 ...

  9. 信奥题库(OI题库)8月月赛T1题解 幂次数

    0.前置知识 分解质因数 快速幂(不必要) 1.思路 首先,我们知道一个正整数(设它为 \(a\) )一定能分解成这样的形式: \[a= \prod_{i\in N^*} p_i^{c_i} \] 其 ...

  10. Homework_1

    寒假第一份作业 一.博客园部分 1.个人博客园链接 Click here 2.markdown界面截图 我用typora编辑 所以贴上typora界面的截图 二.Github部分 1.个人Github ...