이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "game.h"
//#include "grader.cpp"
#include <map>
#include <vector>
#include <cstring>
#include <assert.h>
#include <iostream>
using namespace std;
const int MAXN = 1505;
struct Profile
{
int root1, root2;
int sz1, sz2;
Profile(){}
Profile(int root1, int root2, int sz1, int sz2)
{
this->root1 = root1;
this->root2 = root2;
this->sz1 = sz1;
this->sz2 = sz2;
}
};
bool operator <(Profile A, Profile B)
{
if(A.root1!=B.root1) return A.root1<B.root1;
if(A.root2!=B.root2) return A.root2<B.root2;
if(A.sz1!=B.sz1) return A.sz1<B.sz1;
return A.sz2<B.sz2;
}
struct DSU
{
int parent[MAXN];
vector <int> nodes[MAXN];
DSU(){}
DSU(int n)
{
for(int i = 0;i<n;i++)
{
this->parent[i] = i;
this->nodes[i] = {i};
}
}
int Find(int x)
{
if(parent[x]==x) return x;
parent[x] = Find(parent[x]);
return parent[x];
}
bool Union(int u, int v)
{
u = Find(u);
v = Find(v);
if(u==v) return false;
if(nodes[u].size()<nodes[v].size()) swap(u, v);
parent[v] = u;
for(int x: nodes[v]) nodes[u].push_back(x);
return true;
}
};
Profile makeProfile(int u, int v, DSU &T)
{
u = T.Find(u);
v = T.Find(v);
if(u>v) swap(u, v);
Profile out;
out.root1 = u;
out.root2 = v;
out.sz1 = T.nodes[u].size();
out.sz2 = T.nodes[v].size();
return out;
}
DSU T;
bool asked[MAXN][MAXN];
map <Profile, pair <int, int>> memo;
void initialize(int n)
{
T = DSU(n);
memset(asked, false, sizeof(asked));
}
int evalProfile(Profile p, pair <int, int> &lastInds)
{
int u = p.root1, v = p.root2;
vector <int> &v1 = T.nodes[T.Find(u)];
vector <int> &v2 = T.nodes[T.Find(v)];
int found = 0;
for(int i = lastInds.first;i<v1.size();i++)
{
for(int j = lastInds.second;j<v2.size();j++)
{
if(asked[ v1[i] ][ v2[j] ]==false)
{
lastInds = {i, j};
found++;
break;
}
}
}
return found;
}
int hasEdge(int u, int v)
{
if(T.Find(u)==T.Find(v))
{
return 1;
}
if(asked[u][v]==true) return 0;
Profile p = makeProfile(u, v, T);
if(memo.find(p)==memo.end()) memo[p] = {0, 0};
//cout << "Profile: " << p.root1 << " " << p.root2 << " " << p.sz1 << " " << p.sz2 << '\n';
//cout << memo[p] << " == " << evalProfile(p) << '\n';
//assert(memo[p]==evalProfile(p));
asked[u][v] = true;
asked[v][u] = true;
int found = evalProfile(p, memo[p]);
//cout << memo[p] << '\n';
if(found==0)
{
T.Union(u, v);
return 1;
}
else
{
return 0;
}
}
/*
4
0 2
1 3
0 3
1 2
0 1
2 3
*/
컴파일 시 표준 에러 (stderr) 메시지
game.cpp: In function 'int evalProfile(Profile, std::pair<int, int>&)':
game.cpp:106:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for(int i = lastInds.first;i<v1.size();i++)
| ~^~~~~~~~~~
game.cpp:108:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for(int j = lastInds.second;j<v2.size();j++)
| ~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |