Codeforces Round #479 (Div. 3) E. Cyclic Components (思维,DFS)
题意:给你\(n\)个顶点和\(m\)条边,问它们有多少个单环(无杂环),例如图中第二个就是一个杂环.
题解:不难发现,如果某几个点能够构成单环,那么每个点一定只能连两条边.所以我们先构建邻接表,然后从某个数开始跑dfs,如果这一边所有点的度数都为\(2\),那么就能构成一个单环.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
using namespace std;
typedef pair<int,int> PII;
typedef pair<long,long> PLL; int n,m;
int u,v;
bool st[N];
bool ok=1;
vector<int> V[N]; void dfs(int x){
st[x]=true;
if(V[x].size()!=2) ok=0; for(auto w:V[x]){
if(!st[w]) dfs(w);
}
} int main() {
ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
while(m--){
cin>>u>>v;
V[u].pb(v);
V[v].pb(u);
}
int ans=0;
for(int i=1;i<=n;++i){
ok=1;
if(!st[i]){
dfs(i);
if(ok) ans++;
}
} printf("%d\n",ans); return 0;
}
Codeforces Round #479 (Div. 3) E. Cyclic Components (思维,DFS)的更多相关文章
- Codeforces Round #479 (Div. 3)解题报告
题目链接: http://codeforces.com/contest/977 A. Wrong Subtraction 题意 给定一个数x,求n次操作输出.操作规则:10的倍数则除10,否则减1 直 ...
- Codeforces Round #479 (Div. 3) 题解 977A 977B 977C 977D 977E 977F
A. Wrong Subtraction 题目大意: 定义一种运算,让你去模拟 题解: 模拟 #include <iostream> #include <cstdio> ...
- Codeforces Round #479 (Div. 3)题解
CF首次推出div3给我这种辣鸡做,当然得写份博客纪念下 A. Wrong Subtraction time limit per test 1 second memory limit per test ...
- Codeforces Round #479 (Div. 3)
手速场2333,这群人贼牛逼!手速贼快! A. Wrong Subtraction time limit per test 1 second memory limit per test 256 m ...
- Codeforces Round #479 (Div. 3)解题代码
A. Wrong Subtraction #include <bits/stdc++.h> using namespace std; int main() { int n,k; cin&g ...
- E. Cyclic Components (DFS)(Codeforces Round #479 (Div. 3))
#include <bits/stdc++.h> using namespace std; *1e5+; vector<int>p[maxn]; vector<int&g ...
- Codeforces Round #479 (Div. 3) A. Wrong Subtraction
题目网址:http://codeforces.com/contest/977/problem/A 题解:给你一个数n,进行k次变换,从末尾开始-1,512变成511,511变成510,510会把0消掉 ...
- Codeforces Round #479 (Div. 3) C. Less or Equal
题目地址:http://codeforces.com/contest/977/problem/C 题解:给一串数组,是否找到一个数x,找到k个数字<=x,找到输出x,不能输出-1.例如第二组,要 ...
- Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)
题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...
随机推荐
- Redis Cluster 集群节点维护 (三)
Redis Cluster 集群节点维护: 集群运行很久之后,难免由于硬件故障,网络规划,业务增长,等原因对已有集群进行相应的调整,比如增加redis nodes 节点,减少节点,节点迁移,更换服务器 ...
- java8 stream api流式编程
java8自带常用的函数式接口 Predicate boolean test(T t) 传入一个参数返回boolean值 Consumer void accept(T t) 传入一个参数,无返回值 F ...
- 如何跑通第一个 SQL 作业
简介: 本文由阿里巴巴技术专家周凯波(宝牛)分享,主要介绍如何跑通第一个SQL. 一.SQL的基本概念 1.SQL 分类 SQL分为四类,分别是数据查询语言(DQL).数据操纵语言(DML).数据定义 ...
- Entity与Entity之间的相互转化
一.两个实体类的属性名称对应之间的转化 1.两个实体类 public class Entity1 { private Integer id; private String name; private ...
- Set、Map的区别
应用场景Set用于数据重组,Map用于数据储存Set: (1)成员不能重复(2)只有键值没有键名,类似数组(3)可以遍历,方法有add, delete,hasMap:(1)本质上是健值对的集合,类似集 ...
- Jenkins免密码登录
Jenkins免密码登录 相关内容原文地址: 简书:海边的卡夫卡丶:Jenkins免密码登录 昨天惊现一个神奇的问题,Jenkins无法登录,无论是初始化的账号密码,还是admin用户,都登录不进去了 ...
- JDBC连接Oracle实现增、删、改操作
jsp页面的form表单 AddMentaction实现向数据新增数据操作 DeleteMent删除操作 UpdateMent修改操作 最近在做一个练习项目"在线考试系统",在将整 ...
- Django(ORM单表操作)
默认使用sqllite数据库 修改为mysql数据库 创建数据库 在app models中编写创建数据库类 from django.db import models class Book(models ...
- 一块网卡配2IP地址
我们知道在Linux下网卡被称为eth0,eth1,eth2.....,所有网卡的配置文件都存储在 /etc/sysconfig/network-script/下,文件名是以ifcfg-eth0,if ...
- Spring boot 使用Redis 消息队列
package com.loan.msg.config; import com.loan.msg.service.MessageReceiver; import org.springframework ...