Submission #19719897


Source Code Expand

Copy
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
template <class T>
inline bool chmin(T &a, T b)
{
	if (a > b)
	{
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
	if (a < b)
	{
		a = b;
		return true;
	}
	return false;
}

template <typename T>
T mypow(T x, T n, const T &p = -1)
{ //x^nをmodで割った余り
	T ret = 1;
	while (n > 0)
	{
		if (n & 1)
		{
			if (p != -1)
				ret = (ret * x) % p;
			else
				ret *= x;
		}
		if (p != -1)
			x = (x * x) % p;
		else
			x *= x;
		n >>= 1;
	}
	return ret;
}

using namespace std;
// using namespace atcoder;

void solve()
{
	int n;
	cin>>n;
	vector<vector<int>>g(400005);
	rep(i,n){
		int a,b;
		cin>>a>>b;
		a--;b--;
		g[a].eb(b);
		g[b].eb(a);
	}
	vector<bool>visited(400005);

	int cnt=0,ver=0;

	auto dfs=[&](auto self,int v)->void{
		visited[v]=true;
		++ver;
		for(auto x:g[v]){
			++cnt;
			if(!visited[x]){
				self(self,x);
			}
		}
	};

	int ans=0;

	rep(i,400001){
		if(!visited[i]){
			cnt=0;
			ver=0;
			dfs(dfs,i);
			if(cnt==2*(ver-1)){
				ans+=ver-1;
			}
			else{
				ans+=ver;
			}
		}
	}

	cout<<ans<<"\n";
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}

Submission Info

Submission Time
Task B - Reversible Cards
User tanimanianima364
Language C++ (GCC 9.2.1)
Score 400
Code Size 1932 Byte
Status AC
Exec Time 148 ms
Memory 25240 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 27
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt
Case Name Status Exec Time Memory
0_000.txt AC 19 ms 12596 KB
0_001.txt AC 13 ms 12524 KB
0_002.txt AC 21 ms 12640 KB
1_003.txt AC 16 ms 12512 KB
1_004.txt AC 14 ms 12592 KB
1_005.txt AC 42 ms 15048 KB
1_006.txt AC 16 ms 12604 KB
1_007.txt AC 14 ms 12880 KB
1_008.txt AC 46 ms 15084 KB
1_009.txt AC 141 ms 20564 KB
1_010.txt AC 146 ms 20536 KB
1_011.txt AC 148 ms 20588 KB
1_012.txt AC 68 ms 25240 KB
1_013.txt AC 62 ms 18636 KB
1_014.txt AC 49 ms 14552 KB
1_015.txt AC 54 ms 19632 KB
1_016.txt AC 14 ms 12604 KB
1_017.txt AC 25 ms 13396 KB
1_018.txt AC 45 ms 14780 KB
1_019.txt AC 122 ms 18940 KB
1_020.txt AC 105 ms 18896 KB
1_021.txt AC 15 ms 12672 KB
1_022.txt AC 14 ms 12664 KB
1_023.txt AC 91 ms 17620 KB
1_024.txt AC 57 ms 15780 KB
1_025.txt AC 124 ms 18976 KB
1_026.txt AC 72 ms 16628 KB