首页 > 其他分享 >Atcoder ABC040D 道路の老朽化対策について

Atcoder ABC040D 道路の老朽化対策について

时间:2023-01-08 15:01:28浏览次数:61  
标签:Atcoder le return int siz fa ABC040D qu 老朽

链接
难度:\(\texttt{1656}\)


\(n\) 个点 \(m\) 条无向边,每条边 \((u_i,v_i)\) 有一个对应的时间 \(t_i\)。
\(q\) 次询问,每次询问从 \(x_i\) 只经过 \(t_i>y_i\) 的边共能到达多少个点。

数据范围:\(1\le n,q\le 100000, 0\le m\le 200000,1\le u_i\not v_i\le n,1\le t_i,y_i\le 100000\)。


第一眼什么神仙题,第二眼什么垃圾题。


发现只有 \(t_i>y_i\) 的边才会对本次询问产生影响,考虑对加边和询问离线处理,按时间从大至小用并查集维护连通块大小。

注意因为是 \(t_i>y_i\),同一时间询问需放在合并之前


# include <bits/stdc++.h>
using namespace std;
const int N = 100000, Q = 100000, M = 200000;
int fa [N + 10], siz [N + 10];
int getfa (int x) {
	if (fa [x] ^ x) {
		return fa [x] = getfa (fa [x]);
	}
	return x;
}
void merge (int x, int y) {
	x = getfa (x), y = getfa (y);
	if (x ^ y) {
		if (siz [x] > siz [y]) {
			x ^= y ^= x ^= y;
		}
		fa [x] = y;
		siz [y] += siz [x];// 维护连通块大小
	}
	return ;
}
struct opt {
	int y, a, b, id;
} qu [M + Q + 10];
int ans [Q + 10];
int main () {
	ios :: sync_with_stdio (false);
	cin .tie (0);
	cout .tie (0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++ i) {
		fa [i] = i, siz [i] = 1;
	}
	int m;
	cin >> m;
	for (int i = 1, a, b, y; i <= m; ++ i) {
		cin >> a >> b >> y;
		qu [i] = {y, a, b, 0};
	}
	int q;
	cin >> q;
	for (int i = 1, a, y; i <= q; ++ i) {
		cin >> a >> y;
		qu [m + i] = {y, a, 0, i};
	}// 离线处理
	sort (qu + 1, qu + m + q + 1, [] (opt a, opt b) {
		if (a .y ^ b .y) {
			return a .y > b .y;
		}
		return a .id > b .id;// 同一时间询问在前
	});
	for (int i = 1; i <= m + q; ++ i) {
		if (qu [i] .id) {
			ans [qu [i] .id] = siz [getfa (qu [i] .a)]; 
		}
		else {
			merge (qu [i] .a, qu [i] .b);	
		}
	}
	for (int i = 1; i <= q; ++ i) {
		cout << ans [i] << '\n';
	}
	return 0;
}

标签:Atcoder,le,return,int,siz,fa,ABC040D,qu,老朽
From: https://www.cnblogs.com/lhzQAQ/p/17034674.html

相关文章

  • AtCoder Beginner Contest 284-F - ABCBAC(双哈希)
    F-ABCBAC题目大意:给定一个正整数n,和一个长度为2*n的字符串s问s串能不能是由一个t串经过如下操作变成s串:t串的前i个字符t串的反转串t串的后(n-i)个字符如果存在......
  • AtCoder Beginner Conest 284 解题报告
    AtCoderBeginnerConest284解题报告\(\text{ByDaiRuiChen007}\)\(\text{ContestLink}\)A.SequenceofStrings模拟,时间复杂度\(\Theta(n)\)#include<bits/stdc......
  • 2023.1.7(Atcoder Beginner Contest 284)
    A.HappyNewYear2023Linkhttps://atcoder.jp/contests/abc284/tasks/abc284_dStatement将给定的\(N\)分解成\(N=p^2\cdotq\)的形式,其中\(p,q\)为两个不......
  • Atcoder ABC 284题解
    DHappyNewYear2023(枚举,时间复杂度计算)题意​ 给定\(n\\le\9\times10^{18}\),给出式子\(n=p^2\timesq\),该式子必定有解且有唯一解。请输出\(p\)和\(q\)......
  • Atcoder Beginner Contest ABC 284 Ex Count Unlabeled Graphs 题解 (Polya定理)
    题目链接弱化版(其实完全一样)u1s1,洛谷上这题的第一个题解写得很不错,可以参考直接边讲Polya定理边做这题问题引入:n颗珠子组成的手串,每颗珠子有两种不同的颜色,如果两......
  • Atcoder Beginner Contest ABC 284 Ex Count Unlabeled Graphs 题解 (Polya定理)
    题目链接弱化版(其实完全一样)u1s1,洛谷上这题的第一个题解写得很不错,可以参考直接边讲Polya定理边做这题问题引入:n颗珠子组成的手串,每颗珠子有两种不同的颜色,如果两......
  • Atcoder Beginner Contest 284总结
    前言第一次做出6道题。比赛过程A题白给,耗时\(\text{1min}\)。B题白给,然而突然忘了oddnumber是奇数还是偶数,于是翻译了一下。耗时\(\text{2mins}\)。C题直接......
  • AtCoder Beginner Contest 284
    A-SequenceofStrings(abc284a)题目大意顺序给定\(n\)个字符串,倒着顺序输出。解题思路模拟即可。神奇的代码#include<bits/stdc++.h>usingnamespacestd;u......
  • E - Don't Isolate Elements -- ATCODER
    E-Don'tIsolateElementshttps://atcoder.jp/contests/abc283/tasks/abc283_e 思路 参考https://www.cnblogs.com/cilinmengye/p/17008799.html ......
  • AtCoder Beginner Contest 257 题解
    AtCoderBeginnerContest257Solution目录AtCoderBeginnerContest257Solution更好的阅读体验戳此进入题面链接题面Luogu链接abc跳了[ABC257D]JumpingTakahashi......