이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define sp " "
#define endl "\n"
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define N 200005
const int modulo = 1e9 + 7;
int ans[N], u[N], v[N], l[N], r[N];
vector<int> ind[N], root, gh[N];
map<int, int> res[N];
int find(vector<int> &b, int node)
{
if (node < 0) return -(find(b, -node));
if (node == b[node]) return node;
return b[node] = find(b, b[node]);
}
int32_t main()
{
fastio();
int n, m, q;
cin>>n>>m>>q;
root.resize(n + 5, 0);
for (int i = 1; i <= n; i++)
root[i] = i;
for (int i = 1; i <= m; i++)
{
cin>>u[i]>>v[i];
}
for (int i = 1; i <= q; i++)
{
cin>>l[i]>>r[i];
ind[r[i]].pb(l[i]);
}
for (int i = m; i >= 1; i--)
{
sort(ind[i].begin(), ind[i].end());
if (ind[i].size() > 0)
{
vector<int> tmp(root.begin(), root.end());
gh[i] = tmp;
}
if (ans[i] == 1)
{
ans[i - 1] = 1;
continue;
}
int a = find(root, u[i]), b = find(root, v[i]);
if (a == b) ans[i - 1] = 1;
else if (a < 0) a *= -1, b *= -1;
root[a] = -b;
//cout<<i<<sp<<ans[i]<<endl;
}
for (int i = m; i >= 1; i--)
{
if (ind[i].empty()) continue;
int last = 1;
for (auto j : ind[i])
{
if (ans[i] == 1 || res[i][last] == 1)
{
res[i][j] = 1;
//cout<<i<<sp<<j<<sp<<ans[i]<<sp<<res[i][j]<<endl;
continue;
}
for (int k = last; k < j && res[i][j] == 0; k++)
{
int a = find(gh[i], u[k]), b = find(gh[i], v[k]);
if (a == b) res[i][j] = 1;
else if (a < 0) a *= -1, b *= -1;
gh[i][a] = -b;
}
//cout<<i<<sp<<j<<sp<<res[i][j]<<endl;
last = j;
}
}
for (int i = 1; i <= q; i++)
{
if (res[r[i]][l[i]]) cout<<"YES\n";
else cout<<"NO\n";
}
cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |