이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) (x).begin(),(x).end()
#define inv(n) power((n), mod - 2)
#define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
#define fff(i,a,b) for (int (i) = (a); (i) <= b; (i)++)
#define bff(i,a,b) for (int (i) = (b)-1; (i) >= (a); (i)--)
#define bfff(i,a,b) for (int (i) = (b); (i) >= (a); (i)--)
#define sum_overflow(a,b) __builtin_add_overflow_p ((a), (b), (__typeof__ ((a) + (b))) 0)
#define mul_overflow(a,b) __builtin_mul_overflow_p ((a), (b), (__typeof__ ((a) + (b))) 0)
using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;
//Note to self: Check for overflow
struct disjoint_set_union
{
vector<int> up;
void build(int n)
{
up.resize(n,-1);
}
int Up(int x)
{
if (up[x]<0) return x;
return up[x]=Up(up[x]);
}
bool same(int a, int b)
{
return Up(a)==Up(b);
}
void dsu(int a, int b)
{
a=Up(a),b=Up(b);
if (a==b) return;
up[a]=b;
}
};
bool moze(int n, int* S, int m, int *X, int* Y, int* P, int* Q, int okle)
{
disjoint_set_union want,cur;
want.build(n),cur.build(n);
vector<int> extras;
ff(i,0,n) want.dsu(i,S[i]);
bff(i,0,okle)
{
if (cur.same(X[i],Y[i]) == want.same(X[i],Y[i])) P[i]=X[i],Q[i]=Y[i],extras.pb(i);
else P[i]=0,Q[i]=0,want.dsu(X[i],Y[i]);
}
ff(i,0,n) if (!cur.same(i,want.Up(i)))
{
if (extras.empty()) return false;
int gde=extras.back(); extras.popb();
P[gde]=i,Q[gde]=want.Up(i),want.dsu(i,want.Up(i));
}
ff(i,0,n) if (!want.same(i,cur.Up(i))) return false;
return true;
}
int findSwapPairs(int n, int* S, int m, int* X, int* Y, int* P, int* Q)
{
int l=-1,r=m;
while (r-l>1)
{
int mid=(l+r)/2;
if (moze(n,S,m,X,Y,P,Q,mid)) r=mid;
else l=mid;
}
moze(n,S,m,X,Y,P,Q,r);
return r;
}
컴파일 시 표준 에러 (stderr) 메시지
sorting.cpp: In function 'bool moze(int, int*, int, int*, int*, int*, int*, int)':
sorting.cpp:13:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
13 | #define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
| ^
sorting.cpp:74:5: note: in expansion of macro 'ff'
74 | ff(i,0,n) want.dsu(i,S[i]);
| ^~
sorting.cpp:15:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
15 | #define bff(i,a,b) for (int (i) = (b)-1; (i) >= (a); (i)--)
| ^
sorting.cpp:75:5: note: in expansion of macro 'bff'
75 | bff(i,0,okle)
| ^~~
sorting.cpp:13:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
13 | #define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
| ^
sorting.cpp:81:5: note: in expansion of macro 'ff'
81 | ff(i,0,n) if (!cur.same(i,want.Up(i)))
| ^~
sorting.cpp:13:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
13 | #define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
| ^
sorting.cpp:87:5: note: in expansion of macro 'ff'
87 | ff(i,0,n) if (!want.same(i,cur.Up(i))) return false;
| ^~
sorting.cpp:68:30: warning: unused parameter 'm' [-Wunused-parameter]
68 | bool moze(int n, int* S, int m, int *X, int* Y, int* P, int* Q, int okle)
| ~~~~^
# | 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... |