Untitled
Anonymous
plain_text
02/22/2026 4:46 PM
7.4 KB
9
Indexable
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
using namespace std;
#define fastio \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define int long long
#define ii int, int
#define MP make_pair
#define endl '\n'
#define ll long long
#define lli long long int
#define cy cout << "YES\n"
#define cn cout << "NO\n"
#define fin(i, n) for (int i = 0; i < n; i++)
#define fid(i, n) for (int i = 1; i <= n; i++)
#define vi vector<int>
#define vll vector<ll>
const ll MOD = 1e9+7;
const int MAXN = 6000000;
#define F first
#define S second
// Debugging macros
#define watch(x) cerr << (#x) << " = " << (x) << endl;
#define watch2(x, y) cerr << (#x) << " = " << (x) << ", " << (#y) << " = " << (y) << endl;
#define watchv(v) \
cerr << (#v) << " = "; \
for (auto val : v) \
cerr << val << " "; \
cerr << endl;
// Helper to handle pairs (Key: Value) formatting
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p)
{
return os << "{" << p.first << ": " << p.second << "}";
}
// Forward declaration for nested containers
template <typename T>
void pr_val(const T &x);
// Main logic for STL containers (Vectors, Sets, Maps)
template <typename T>
auto pr_stl(const T &container) -> decltype(container.begin(), void())
{
cerr << "[";
bool first = true;
for (const auto &x : container)
{
if (!first)
cerr << ", ";
pr_val(x); // Recursive call for nested STL
first = false;
}
cerr << "]";
}
// Base case for primitive types and strings
template <typename T>
void pr_val(const T &x)
{
if constexpr (is_same_v<T, string>)
cerr << '"' << x << '"';
else
cerr << x;
}
// Specialization for STL structures to trigger the list formatting
template <typename T>
void pr_val(const vector<T> &v)
{
pr_stl(v);
}
template <typename T>
void pr_val(const set<T> &s)
{
pr_stl(s);
}
template <typename K, typename V>
void pr_val(const map<K, V> &m)
{
pr_stl(m);
}
// #x captures the variable name, pr_val handles the value/structure
#define pr(x) \
cerr << #x << " = "; \
pr_val(x); \
cerr << endl;
// Factorials and nCr precomputation
vector<ll> fact(MAXN), invfact(MAXN);
ll mod_pow(ll a, ll b, ll mod)
{
ll res = 1;
a %= mod;
while (b > 0)
{
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
ll mod_inv(ll a, ll mod = MOD)
{
return mod_pow(a, mod - 2, mod);
}
void precomputeFactorials()
{
fact[0] = 1;
for (int i = 1; i < MAXN; i++)
fact[i] = fact[i - 1] * i % MOD;
invfact[MAXN - 1] = mod_inv(fact[MAXN - 1]);
for (int i = MAXN - 2; i >= 0; i--)
invfact[i] = invfact[i + 1] * (i + 1) % MOD;
}
ll nCr(int n, int r)
{
if (r < 0 || r > n)
return 0;
return fact[n] * invfact[r] % MOD * invfact[n - r] % MOD;
}
// Bit manipulation helpers
#define set_bit(x, i) ((x) | (1LL << (i)))
#define clear_bit(x, i) ((x) & ~(1LL << (i)))
#define toggle_bit(x, i) ((x) ^ (1LL << (i)))
#define check_bit(x, i) ((x) & (1LL << (i)))
#define lsb(x) ((x) & -(x))
int count_bits(int n)
{
return __builtin_popcountll(n);
}
// Sieve to compute smallest prime factor for all numbers
int spf[MAXN + 1];
void sieve()
{
for (int i = 0; i <= MAXN; i++)
spf[i] = i;
for (int i = 2; i * i <= MAXN; i++)
{
if (spf[i] == i)
{
for (int j = i * i; j <= MAXN; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
// Get prime factorization of x using SPF
vector<pair<int, int>> getFactorization(int x)
{
vector<pair<int, int>> res;
while (x != 1)
{
int p = spf[x], cnt = 0;
while (x % p == 0)
{
x /= p;
cnt++;
}
res.push_back({p, cnt});
}
return res;
}
// Get all factors/divisors of n
vector<int> getFactors(int n)
{
vector<int> factors;
for (int i = 1; i * i <= n; i++)
{
if (n % i == 0)
{
factors.push_back(i);
if (i != n / i)
factors.push_back(n / i);
}
}
sort(factors.begin(), factors.end());
return factors;
}
// GCD and LCM
int gcd(int a, int b)
{
return __gcd(a, b);
}
int lcm(int a, int b)
{
return a / __gcd(a, b) * b;
}
// Check if n is prime
bool isPrime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i += 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
// Custom sort for pairs: descending first, ascending second
template <typename T1, typename T2>
bool customPairSort(const pair<T1, T2> &a, const pair<T1, T2> &b)
{
if (a.F != b.F)
return a.F > b.F;
return a.S < b.S;
}
// Fast modular exponentiation
ll power(ll a, ll b, ll mod)
{
ll result = 1;
a %= mod;
while (b > 0)
{
if (b & 1)
result = result * a % mod;
a = a * a % mod;
b >>= 1;
}
return result;
}
vector<set<int>> g;
void solve() {
int n, m;
cin >> n >> m;
g.resize(n);
for(int i=0; i<m; i++){
int u, v;
cin >> u >> v;
u--; v--;
if(u==v) continue;
g[u].insert(v);
}
set<int> curr;
set<int> next;
next.insert(0);
for(int i=0; i<n; i++){
if(next.find(i)==next.end()){
cout << "-1 ";
continue;
}
curr.insert(i);
next.erase(i);
}
cout << "\n";
}
signed main()
{
fastio;
// int t;
// cin >> t;
// while (t--)
{
solve();
}
return 0;
}Editor is loading...
Leave a Comment