首页 > 其他分享 >题解 ABC293D【Tying Rope】

题解 ABC293D【Tying Rope】

时间:2023-03-12 09:14:24浏览次数:34  
标签:cntE vis int 题解 ++ Rope cntV Tying define

颜色是不好处理的,我们不妨不区分绳子的两个端点,将每条绳子作为一个节点,每条边直接将两个节点连接起来。每个绳子的端点本质上是保证了每个点的度数不超过 \(2\),也就是说图的每个连通块要么是环要么是链。题目即统计有多少个环、多少个链。

洪水填充统计每个连通块的点数、边数即可。

时间复杂度 \(\mathcal O(n+m)\)。

// Problem: D - Tying Rope
// Contest: AtCoder - AtCoder Beginner Contest 293
// URL: https://atcoder.jp/contests/abc293/tasks/abc293_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do{freopen(s".in","r",stdin);freopen(s".out","w",stdout);}while(false)
#define likely(exp) __builtin_expect(!!(exp), 1)
#define unlikely(exp) __builtin_expect(!!(exp), 0)
using namespace std;
typedef long long ll;

mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
int randint(int L, int R) {
	uniform_int_distribution<int> dist(L, R);
	return dist(rnd);
}

template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}

const int N = 2e5+5;

int n, m, vis[N], cntV, cntE, ansA, ansB;
vector<int> e[N];

void dfs(int u) {
	vis[u] = 1;
	++cntV;
	for(int v : e[u]) {
		if(!vis[v]) dfs(v);
		++cntE;
	}
}

int main() {
	scanf("%d%d", &n, &m);
	rep(i, 1, m) {
		int u, v; char s[2], t[2];
		scanf("%d%s%d%s", &u, s, &v, t);
		e[u].push_back(v);
		e[v].push_back(u);
	}
	rep(i, 1, n) {
		if(!vis[i]) {
			cntV = cntE = 0;
			dfs(i);
			cntE >>= 1;
			if(cntV == cntE) ++ansA;
			else ++ansB;
		}
	}
	printf("%d %d\n", ansA, ansB);
	return 0;
}

标签:cntE,vis,int,题解,++,Rope,cntV,Tying,define
From: https://www.cnblogs.com/ruierqwq/p/abc293d.html

相关文章

  • 题解 ABC293E【Geometric Progression】
    由于模数不一定是大质数,我们不能直接套等比数列求和公式。换一种思路,数列\(\langle1,A,A^2,\cdots,A^{X-1}\rangle\)可以看做线性递推,因此设计矩阵:\[\boldsymbolT=\b......
  • 题解 ABC293F【Zero or One】
    我们可以暴力检查进制数不超过\(B\)的是否符合要求;然后对于进制数大于\(B\)的,位数不超过\(\log_BN\),可以暴力枚举每一位的值然后二分进制数检查。代码中\(B=10^3\)......
  • 题解 ABC293G【Triple Index】
    莫队板子。类似于小B的询问,在移动指针过程中,维护每个数出现次数\(cnt_i\),同时维护\(\sum\binom{cnt_i}{3}\)即可。取序列分块块长\(B=\frac{n}{\sqrt{m}}\),有最优......
  • [ABC293E] Geometric Progression 题解
    [ABC293E]GeometricProgression题解神中神数论题目描述给定整数\(A,X,M\),求\[\sum_{i=0}^{X-1}A^i\bmodM\]\(1\leA,M\le10^9\)\(1\leX\le10^......
  • UVA12107 题解
    前言题目传送门!更好的阅读体验?很久以前的一道搜索大模拟题目,另一篇题解的写法有点鬼畜,所以就来补篇题解。题面给你一个数字谜。修改最少次数(每次修改一个数位为空格或......
  • [POI2001][HAOI2007] 反素数 题解
    前置知识:一些关于约数的小常识。唯一分解定理对于所有正整数\(n\),一定有唯一分解方式\(n=p_1^{c_1}p_2^{c_2}\cdotsp_m^{c_m}\),其中\(p_1<p_2<\cdots<p_m\),......
  • P3530[POI2012 FES-Festival] 题解
    题面链接简要题意对于数列\(\{v_n\}\),有两种约束\(v_i=v_j+1\)和\(v_i\gev_j\),问\(\{v_n\}\)最多有多少个不同的项。解法考虑先建图,注意到如果约束图是DAG,那么......
  • CF1795 G.Removal Sequences - 题解
    记\(N(u)\)表示图上与点\(u\)相邻的点,\(p_u=deg_u-a_u\),其中\(deg_u\)为无向图上点\(u\)的度数。首先要删除\(p_u=0\)的点,同时\(\forallv\inN(u),p_v......
  • properties乱码
    在这里不能改......
  • CF888D Almost Identity Permutations 题解
    CF链接:AlmostIdentityPermutationsLuogu链接:AlmostIdentityPermutations${\scr\color{Aquamarine}{\text{Solution}}}$前言这好像是一道能用数学秒掉的题目但......