列表

详情


NC207197. 聚会

描述

牛牛有n个朋友,标号为0到n-1,每一个朋友最多有一个不喜欢的人,记为a[i],如果第i个人喜欢每一个人,则a[i]=i

现在牛牛需要邀请每一个人来参加一个聚会,但是他发现不同的邀请顺序会导致不同的结果,因为一旦某个人发现他不喜欢的人已经被邀请了
他就不会来参加聚会,请问最多可以邀请多少个人来参加聚会

输入描述

第一行输入一个整数n (1≤n≤50)

第二行输入n个整数
ai 0≤ai≤n−1


输出描述

输出一个整数

示例1

输入:

2
0 1

输出:

2

示例2

输入:

2
1 0

输出:

1

示例3

输入:

5
4 3 2 1 0

输出:

3

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++11) 解法, 执行用时: 3ms, 内存消耗: 388K, 提交时间: 2021-02-21 09:03:44

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
int IOS = []() {ios::sync_with_stdio(0); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0; }();
auto dbout = [](auto&& ... args) -> void { ((cerr << args << ", "), ...); };
#ifdef CF_DEBUG
#define debug(...) cerr <<"( "<< #__VA_ARGS__ << " ) = ( ", dbout(__VA_ARGS__), cerr << ")\n"
#define debugt(x) cerr << #x<<" = [ "; apply(dbout, x); cerr << "]\n";
#define cin fin
#define cout fout
#define FIN fin.close()
#define FOUT fout.close()
ifstream fin("CF_data.in");
ofstream fout("CF_data.out"); //ios::app
//int Separator = []() { fout << "\n******************\n"; return 0; }();
#else
#define debug(...)
#define debugt(x)
#define FIN
#define FOUT
#endif

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vc<T>>;
template <class T> using pq = priority_queue<T, vector<T>, less<T> >;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T> >; 
#define all(a) begin(a), end(a)
#define SUM(a) accumulate(all(a), 0LL)
#define MIN(a) (*min_element(all(a)))
#define MAX(a) (*max_element(all(a)))
#define lb(a, x) distance((a).begin(), lower_bound(all(a), (x)))
#define ub(a, x) distance((a).begin(), upper_bound(all(a), (x)))

#define toset(a) a.erase(unique(all(a)), a.end())
#define setb(a,b,c) set_union(all(a), all(b), inserter(c, c.begin()))
#define setb_merge(a,b,c) merge(all(a), all(b), inserter(c, c.begin()))
#define setj(a,b,c) set_intersection(all(a), all(b), inserter(c, c.begin()))
#define setc(a,b,c) set_difference(all(a), all(b), inserter(c, c.begin()))
#define setc_sym(a,b,c) set_symmetric_difference(all(a), all(b), inserter(c, c.begin()))

constexpr int INF = 0x3f3f3f3f;
constexpr ll MOD = 1e9 + 7;

template<typename T> T gcd(T a, T b)
{ if (a < b) swap(a, b); if (b) while (b ^= a ^= b ^= a %= b); return a; }
template<typename T> T exgcd(T a, T b, T& x, T& y)
{ T d = a; if (b) d = extgcd(b, a % b, y, x), y -= (a / b) * x; else x = 1, y = 0; return d; }
ll POW(ll a, ll b, ll p = MOD)
{ ll r = 1; for (a %= p; b > 0; a = a * a % p, b >>= 1) if (b & 1) r = r * a % p; return r; }
ll INV(ll a, ll p = MOD) { return POW(a, p - 2, p); }

template<typename T1, typename T2> pair<T1, T2>& operator+=(pair<T1, T2>& p1, const pair<T1, T2>& p2)
{ p1.first += p2.first, p1.second += p2.second; return p1;}
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2>& p1, const pair<T1, T2>& p2)
{ return pair<T1, T2>{p1.first+p2.first, p1.second+p2.second}; }
template<typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p)
{ return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T> ostream& operator<<(ostream& os, const vc<T>& a)
{ for (auto& v : a) os << v << ' '; return os;}
template<typename T> ostream& operator<<(ostream& os, const set<T>& a)
{ for (auto& v : a) os << v << ' '; return os;}
template<typename T> istream& operator>>(istream& is, vc<T>& a)
{ for (auto& v : a) is >> v; return is;}

template<typename T> const T& minimum(const T& n) { return n; }
template<typename T> const T& maximum(const T& n) { return n; }
template<typename T, typename...Args>const T& minimum(const T& n, const Args&...args) 
{ return std::min(n, minimum(args...)); }
template<typename T, typename...Args>const T& maximum(const T& n, const Args&...args) 
{ return std::max(n, maximum(args...)); }

/***********************************************/
constexpr int N = 1e5 + 5; //6e7
constexpr ll mod = 998244353;
ll n, m, k;
vi a;
void solve()
{
	cin >> n;
	a.resize(n);
	cin >> a;
	vi vis(n, 0);
	int ans = n;
	for (int i = 0; i < n; i++)
	{
		if (!vis[i])
		{
			debug(i);
			for (int u = i; u != a[u]; u = a[u])
			{
				vis[u] = i + 1;
				if (vis[a[u]])
				{
					if(vis[a[u]] == i + 1) ans--;
					break;
				}
			}
		}
	}
	cout << ans;
}

/***********************************************/

int main()
{
	int T = 1;
//	cin >> T;
	while (T--) solve();
	cout.flush();
	FIN; FOUT;
	return 0;
}

上一题