結果

問題 No.2316 Freight Train
ユーザー EtisEtis
提出日時 2023-05-26 21:31:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 6,208 bytes
コンパイル時間 2,113 ms
コンパイル使用メモリ 200,880 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 10:29:17
合計ジャッジ時間 5,722 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 63 ms
4,380 KB
testcase_04 AC 34 ms
4,380 KB
testcase_05 AC 24 ms
4,380 KB
testcase_06 AC 7 ms
4,376 KB
testcase_07 AC 43 ms
4,376 KB
testcase_08 AC 43 ms
4,376 KB
testcase_09 AC 43 ms
4,380 KB
testcase_10 AC 44 ms
4,380 KB
testcase_11 AC 46 ms
4,380 KB
testcase_12 AC 53 ms
4,376 KB
testcase_13 AC 64 ms
4,380 KB
testcase_14 AC 64 ms
4,380 KB
testcase_15 AC 66 ms
4,376 KB
testcase_16 AC 67 ms
4,376 KB
testcase_17 AC 65 ms
4,376 KB
testcase_18 AC 68 ms
4,380 KB
testcase_19 AC 67 ms
4,380 KB
testcase_20 AC 66 ms
4,376 KB
testcase_21 AC 66 ms
4,376 KB
testcase_22 AC 65 ms
4,376 KB
testcase_23 AC 49 ms
4,380 KB
testcase_24 AC 58 ms
4,376 KB
testcase_25 AC 51 ms
4,376 KB
testcase_26 AC 50 ms
4,376 KB
testcase_27 AC 27 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC target("avx")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

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

//local debug
#ifdef LOCAL
#include <debug_print.hpp>
#define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (static_cast<void>(0))
#endif

#define overload4(a, b, c, d, e, ...) e

//alias
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

//constants
constexpr const long long MOD = 998244353;
constexpr const long long MODM = 1000000007;
constexpr const int INF = 1e9;
constexpr const ll LINF = 1e18;

//rep(for-loop) macro
#define rep2(i, n) for(ll i = 0; i < n; i++)
#define rep3(i, k, n) for(ll i = k; i < n; i++)
#define rep4(i, k, n, a) for(ll i = k; i < n; i += a)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2)(__VA_ARGS__)
#define rep1_2(i, n) for(ll i = 1; i <= n; i++)
#define rep1_3(i, k, n) for(ll i = k; i <= n; i++)
#define rep1_4(i, k, n, a) for(ll i = k; i <= n; i += a)
#define rep1(...) overload4(__VA_ARGS__, rep1_4, rep1_3, rep1_2)(__VA_ARGS__)
#define Rep2(i, n) for(ll i = n - 1; i >= 0; i--)
#define Rep3(i, k, n) for(ll i = n - 1; i >= k; i--)
#define Rep4(i, k, n, a) for(ll i = n - 1; i >= k; i -= a) 
#define Rep(...) overload4(__VA_ARGS__, Rep4, Rep3, Rep2)(__VA_ARGS__)
#define Rep1_2(i, n) for(ll i = n; i >= 1; i--)
#define Rep1_3(i, k, n) for(ll i = n; i >= k; i--)
#define Rep1_4(i, k, n, a) for(ll i = n; i >= k; i -= a) 
#define Rep1(...) overload4(__VA_ARGS__, Rep1_4, Rep1_3, Rep1_2)(__VA_ARGS__)
#define vfor(v, x) for(auto x : v)
#define mfor(map) for(auto &[key, value] : map)

//vector macro
#define vvecc(T, name, n, m) vector<vector<T>> name(n, vector<T>(m))
#define vvec(T, name, n) vector<vector<T>> name(n)
#define vvecs(T, name, n, m, s) vector<vector<T>> name(n, vector<T>(m, s))
#define all(x) begin(x), end(x)
#define LB(v, x) distance((v).begin(), lower_bound(all(v), (x)))
#define UB(v, x) distance((v).begin(), upper_bound(all(v), (x)))

//data structure macro
#define ef emplace_front
#define eb emplace_back
#define pf pop_front
#define pb pop_back
#define mp make_pair
#define fi first
#define se second
#define mt make_tuple
#define get(t, x) get<x - 1>(t)
#define lb lower_bound
#define ub upper_bound
template<class T> using pq = priority_queue<T>;
template<class T> using pqmin = priority_queue<T, vector<T>, greater<T>>;
template<class T, class U> using pqp = priority_queue<pair<T, U>>;
template<class T, class U> using pqpmin = priority_queue<pair<T, U>, vector<pair<T, U>>, greater<pair<T,U>>>;

//output
#define pl() cout << '\n'
template<class T> void print(const T& a) {cout << a;}
void printl(){}
template<class T> void printl(const T& t){print(t); pl();}
template<class Head, class... Tail> void printl(const Head& head, const Tail&... tail) {print(head); cout << " "; printl(tail...);}
template<class T> void fin(const T& t) {printl(t); exit(0);}
void Yes(bool a) {cout << (a ? "Yes" : "No") << '\n';}
template<class T, class U> void Out2(bool a, T yes, U no) {if(a) printl(yes); else printl(no);}

//functions
int ctoi(char c) {return c - '0';}
char to_char(int x) {return x + '0';}
template<class... T> constexpr auto Emin(T... a) {return min(initializer_list<common_type_t<T...>>{a...});}
template<class... T> constexpr auto Emax(T... a) {return max(initializer_list<common_type_t<T...>>{a...});}
template<class T, class U> bool chmax(T &a, const U &b) {if (a < b) { a = b; return true;} return false;}
template<class T, class U> bool chmin(T &a, const U &b) {if (a > b) { a = b; return true;} return false;}
template<class T, class U> ll Epow(T x, U y) {ll ans = 1; for(int i = 0; i < y; i++) ans *= x; return ans;}
template<class T, class U> ll Eceil(T x, U y) {return (ll)ceil((ld)x / (ld)y);}
template<class T, class U> ll Efloor(T x, U y) {return (ll)floor((ld)x / (ld)y);}
template<class T, class U> bool check_bit(T tar, U bit) {return ((tar & Epow(2, bit)) != 0);}

class UnionFind
{
public:

	UnionFind() = default;

	/// @brief Union-Find 木を構築します。
	/// @param n 要素数
	explicit UnionFind(size_t n)
		: m_parentsOrSize(n, -1) {}

	/// @brief 頂点 i の root のインデックスを返します。
	/// @param i 調べる頂点のインデックス
	/// @return 頂点 i の root のインデックス
	int find(int i)
	{
		if (m_parentsOrSize[i] < 0)
		{
			return i;
		}

		// 経路圧縮
		return (m_parentsOrSize[i] = find(m_parentsOrSize[i]));
	}

	/// @brief a のグループと b のグループを統合します。
	/// @param a 一方のインデックス
	/// @param b 他方のインデックス
	void merge(int a, int b)
	{
		a = find(a);
		b = find(b);

		if (a != b)
		{
			// union by size (小さいほうが子になる)
			if (-m_parentsOrSize[a] < -m_parentsOrSize[b])
			{
				std::swap(a, b);
			}

			m_parentsOrSize[a] += m_parentsOrSize[b];
			m_parentsOrSize[b] = a;
		}
	}

	/// @brief a と b が同じグループに属すかを返します。
	/// @param a 一方のインデックス
	/// @param b 他方のインデックス
	/// @return a と b が同じグループに属す場合 true, それ以外の場合は false
	bool connected(int a, int b)
	{
		return (find(a) == find(b));
	}

	/// @brief i が属するグループの要素数を返します。
	/// @param i インデックス
	/// @return i が属するグループの要素数
	int size(int i)
	{
		return -m_parentsOrSize[find(i)];
	}

private:

	// m_parentsOrSize[i] は i の 親,
	// ただし root の場合は (-1 * そのグループに属する要素数)
	std::vector<int> m_parentsOrSize;
};

//---------------------------------------------------------------------------------------------------
int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(10);

    int N, Q;
    cin >> N >> Q;

    UnionFind uf(N + 1);
    rep1(i, N) {
        int P;
        cin >> P;
        if(P != -1) uf.merge(i, P);
    }

    while(Q--) {
        int A, B;
        cin >> A >> B;

        Yes(uf.connected(A, B));
    }
}



    
0