首页 > 其他分享 >Codeforces Round #346 (Div. 2)-E. New Reform

Codeforces Round #346 (Div. 2)-E. New Reform

时间:2023-06-12 17:32:33浏览次数:58  
标签:yi Reform int Codeforces 346 roads input cities road


原题链接


E. New Reform



time limit per test



memory limit per test



input



output


n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed

The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).

separate, if no road leads into it, while it is allowed to have roads leading from this city.

Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.


Input



n and m — the number of the cities and the number of roads in Berland (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000).

m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers xi, yi (1 ≤ xi, yi ≤ nxi ≠ yi), where xi and yi are the numbers of the cities connected by the i-th road.

It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.


Output



Print a single integer — the minimum number of separated cities after the reform.


Examples



input



4 3 2 1 1 3 4 3



output



1



input



5 5 2 1 1 3 2 3 2 5 4 3



output



0



input



6 5 1 2 2 3 4 5 4 6 5 6



output



1


遍历每个点,若该点未被访问,则从该点i开始搜索,搜出i能到达的所有点,打上标记,这些点都有边指向自己.现在就是判断有没有边指向i,若从i搜索出的连通图是环,或者i在搜索中有数次被访问则有边指向i,否则没有

#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;

vector<int> v[maxn];
bool vis[maxn];
int cnt;
void dfs(int j, int f){
	for(int i = 0; i < v[j].size(); i++){
		int h = v[j][i];
		if(h != f){
			if(vis[h])
				cnt = 1;
			else{
				vis[h] = true;
			    dfs(h, j);
			}
		}
	}
}
int main(){
//	freopen("in.txt", "r", stdin);
	int n, m, a, b;
	scanf("%d%d", &n, &m);
	for(int i = 0; i < m; i++){
		scanf("%d%d", &a, &b);
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ans = 0;
	for(int i = 1; i <= n; i++){
		if(vis[i] == false){
			cnt = 0;
			dfs(i, -1); 
			if(vis[i] == false && cnt == 0)
			 ans++;
		} 
	}
	cout << ans << endl;
	return 0;
}





标签:yi,Reform,int,Codeforces,346,roads,input,cities,road
From: https://blog.51cto.com/u_16158872/6464284

相关文章

  • Codeforces Round #291 (Div. 2)-D. R2D2 and Droid Army(RMQ)
    原题链接D.R2D2andDroidArmytimelimitpertestmemorylimitpertestinputoutputAnarmyof n droidsislinedupinonerow.Eachdroidisdescribedby m integers a......
  • Codeforces Beta Round #22 (Div. 2 Only)-D. Segments
    原题链接D.SegmentstimelimitpertestmemorylimitpertestinputoutputYouaregiven nInputThefirstlineoftheinputcontainssingleintegernumber n (1 ≤ n ......
  • Codeforces Round #190 (Div. 2)-C. Ciel and Robot
    原题链接C.CielandRobottimelimitpertestmemorylimitpertestinputoutputs.Eachcharacterof sU':goup,(x,y)  → D':godown,(x,y)  → L':goleft,(x,y)  → R':goright,(x,y) ......
  • Codeforces Round #362 (Div. 2)-D. Puzzles
    原题链接D.PuzzlestimelimitpertestmemorylimitpertestinputoutputBarneylivesincountryUSC(UnitedStatesofCharzeh).USChas n citiesnumberedfrom 1 through......
  • Codeforces Beta Round #29 (Div. 2, Codeforces format)-D. Ant on the Tree
    原题链接D.AntontheTreetimelimitpertestmemorylimitpertestinputoutputConnectedundirectedgraphwithoutcyclesiscalledatree.Treesisaclassofgraphswhic......
  • CodeForces 4B Before an Exam(DP)
    思路:令dp[i][j]为做了i天用了j时间,然后随便转移一下就可以了#include<cstdio>#include<queue>#include<cstring>#include<iostream>#include<cstdlib>#include<algorithm>#include<vector>#include<map>#include<string>#......
  • CodeForces 4D Mysterious Present(DP)
    题意:你有一张长宽为x,y的卡片同时有n个盒子,长宽分别为xi,yi。然后问你卡片最多塞多少层盒子并且把这些盒子按照从里到外输出。思路:由于数据给小了,所以n^2的DP也是可以水过的~#include<iostream>#include<cstdio>usingnamespacestd;constintmaxn=5005;intx[maxn],y[maxn]......
  • CodeForces 645A Amity Assessment
    题意:给你一个2X2的华容道你,问能不能通过初始给出的棋盘然后变换到最后的棋盘思路:由于是一个2X2的...所以怎么做都可以..留意到每个棋子的移动其实都是顺时针或者逆时针的就好做了。#include<cstdio>#include<queue>#include<cstring>#include<iostream>#include<cstdlib>......
  • Codeforces Round 877 (Div. 2)
    Preface补题这场补题的时候直接成腐乳了,A题挂两发,B题挂两发,C题挂一发是真的抽象虽然D是个套路题看一眼就秒了,但如果真的比赛时候打真要罚时爆炸了的说后面的EF还是做不来啊岂可修,不过都很有启发性让人感觉神清气爽,不像傻逼蓝桥杯花钱买罪受A.BlackboardList刚开始想错方......
  • Codeforces Round 876 Div2 A-D题解
    CodeforcesRound876Div2A-D题解A.TheGoodArray这个题就是问你对于\(i\leqn\),要求前面后面至少\(ceil(\frac{i}{k})\)个1那我们就贪心的每k个放一个1,或者直接用数学算一下就好了AC代码#include<bits/stdc++.h>usingnamespacestd;constexprintlimit=(......