178882025-09-22 16:50:34ubormaciTestnevelés óracpp17Elfogadva 50/50199ms25652 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
//#pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*

notes:

int64_t
stoi(string s) -> string to int
to_string() -> int (or else) to string

vector declaration:
vector<ll> v(n, 0)
vector<vector<ll>> v(n, vector<ll>(n, 0));

{if statement} ? {truth value} : {false value}

set lower bound/upper bound:
 	// . . . m1 . . . d . . . . m2
    auto m1_it = b.lower_bound(d);
    advance(m1_it, -1);
    m1 = *m1_it;
	m2 = *b.upper_bound(d);

#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif

constexpr auto lcm(auto x, auto... xs)
{
	return ((x = std::lcm(x, xs)), ...);
}

std::gcd(int a, int b)

cout << setprecision(n);

set with custom ordering
set<ll, decltype(&cmp)> qu(cmp);

*/

typedef int64_t ll;

void solve() {
	
	ll n, k;
	cin >> n >> k;

	vector<set<ll>> out(n+1);
	vector<ll> in(n+1);

	for(ll i = 0; i < k; i++) {
		ll smaller, taller;
		cin >> smaller >> taller;
		in[taller]++;
		out[smaller].insert(taller);
	}

	queue<ll> q;
	for(ll i = 1; i <= n; i++) {
		if(in[i] == 0) {
			q.push(i);
		}
	}

	vector<ll> order(n, 0);
	ll ordind = 0;

	while(!q.empty()) {

		ll curr = q.front();
		q.pop();

		order[ordind] = curr;
		ordind++;

		for(const auto & ou : out[curr]) {
			
			in[ou]--;
			if(in[ou] == 0) {
				q.push(ou);
			}
		}
	}

	if(ordind < n) {
		cout << "0";
		return;
	}

	// check if more than 1 ordering is possible
	// or, alternatively, if the current ordering is a hamiltonian path
	// hogyha valamelyik elem elotti elem nem a szuloje, akkor a ketto helyet fel lehet cserelni

	ll a = -1, b = -1;

	for(ll i = 1; i < n; i++) {

		if(out[order[i-1]].count(order[i]) == 0) {
			a = order[i];
			b = order[i-1];
			break;
		}
	}

	if(a == -1) {
		cout << "1\n";
		for(ll i = 0; i < n; i++)
		{
			cout << order[i] << " ";
		}
	}else{
		cout << "2\n";
		for(ll i = 0; i < n; i++)
		{
			cout << order[i] << " ";
		}
		for(ll i = 0; i < n; i++)
		{
			if(order[i] == a) {
				cout << b << " ";
			}else if(order[i] == b) {
				cout << a << " ";
			}else{
				cout << order[i] << " ";
			}
		}
	}
 
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	//cin.tie(nullptr);
	//cout.tie(nullptr);

	solve();

	return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms500 KiB
2Elfogadva0/01ms316 KiB
3Elfogadva0/0199ms17204 KiB
4Elfogadva2/21ms316 KiB
5Elfogadva3/31ms316 KiB
6Elfogadva3/31ms316 KiB
7Elfogadva3/31ms316 KiB
8Elfogadva1/11ms316 KiB
9Elfogadva3/31ms316 KiB
10Elfogadva3/32ms576 KiB
11Elfogadva3/32ms564 KiB
12Elfogadva1/12ms564 KiB
13Elfogadva2/22ms580 KiB
14Elfogadva3/31ms536 KiB
15Elfogadva1/1135ms13216 KiB
16Elfogadva3/3122ms15728 KiB
17Elfogadva5/546ms17084 KiB
18Elfogadva1/1188ms25652 KiB
19Elfogadva2/2172ms13108 KiB
20Elfogadva3/3166ms18592 KiB
21Elfogadva4/4129ms18524 KiB
22Elfogadva4/4119ms18740 KiB