이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define task "F"
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 2e5 + 5;
const ll INF = 1e18 + 5;
int n, m, q;
pair<int, int> es[MAXN];
void Input()
{
cin >> n >> m >> q;
for (int i = 1; i <= m; i++)
cin >> es[i].fi >> es[i].se;
}
struct TRback
{
int u, lu, v, lv;
};
stack<TRback> Q;
int lab[MAXN], f[MAXN];
bool pare[MAXN];
pair<int, int> FindSet(int u)
{
bool w = 0;
while (lab[u] > 0)
{
w ^= pare[u];
u = lab[u];
}
return {u, w};
}
bool Unite(int u, int v)
{
auto pu = FindSet(u);
auto pv = FindSet(v);
if (pu.fi == pv.fi)
{
return (pu.se != pv.se);
}
if (lab[pu.fi] > lab[pv.fi])
swap(pu, pv);
u = pu.fi;
v = pv.fi;
Q.push({u, lab[u], v, lab[v]});
lab[u] += lab[v];
lab[v] = u;
pare[v] = pu.se ^ pv.se ^ 1;
return 1;
}
void rollback(int sz)
{
while (Q.size() > sz)
{
auto p = Q.top();
Q.pop();
lab[p.u] = p.lu;
lab[p.v] = p.lv;
}
}
void Dnc(int l, int r, int pl, int pr)
{
if (l > r)
return;
int mid = (l + r) / 2;
int s = Q.size();
bool kt = 1;
for (int i = l; i <= mid; i++)
{
if (!Unite(es[i].fi, es[i].se))
{
kt = 0;
break;
}
}
if (kt)
{
int s1 = Q.size();
for (int i = min(m, pr); i >= max(mid + 1, pl); i--)
{
if (!Unite(es[i].fi, es[i].se))
{
f[mid] = i;
break;
}
}
//if (mid == 1)
// cerr << f[mid] << '\n';
rollback(s1);
Dnc(mid + 1, r, f[mid], pr);
}
rollback(s);
for (int i = f[mid] + 1; i <= pr; i++)
Unite(es[i].fi, es[i].se);
Dnc(l, mid - 1, pl, f[mid]);
rollback(s);
}
void Solve()
{
memset(lab, -1, sizeof(lab));
fill(f, f + 1 + m, m + 1);
Dnc(1, m, 1, m);
memset(lab, -1, sizeof(lab));
for (int i = m; i >= 1; i--)
{
if (!Unite(es[i].fi, es[i].se))
{
f[0] = i;
break;
}
}
while (q-- > 0)
{
int l, r;
cin >> l >> r;
l--;
r++;
cout << (r <= f[l] ? "YES\n" : "NO\n");
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen(task".INP","r",stdin);
//freopen(task".OUT","w",stdout);
Input();
Solve();
}
컴파일 시 표준 에러 (stderr) 메시지
Joker.cpp: In function 'void rollback(int)':
Joker.cpp:64:21: warning: comparison of integer expressions of different signedness: 'std::stack<TRback>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
64 | while (Q.size() > sz)
| ~~~~~~~~~^~~~
# | 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... |