QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#370084 | #2830. Data Structure | PetroTarnavskyi | RE | 0ms | 0kb | C++20 | 3.1kb | 2024-03-28 21:47:23 | 2024-03-28 21:47:23 |
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int N = 1 << 18;
int n, m;
VI poses[N];
VI freeCol;
int cnt[N];
VI component;
VI g[N], gr[N];
bool used[N];
void dfs(int v)
{
used[v] = 1;
component.PB(v);
for(int to : g[v])
if(!used[to])
dfs(to);
}
vector<PII> ans;
VI cols[N];
int whereUp(int v)
{
for(int col : cols[v])
{
if(poses[col].back() == v)
return col;
}
assert(0);
}
void delCol(int v, int col)
{
int id = -1;
FOR(i, 0, SZ(cols[v]))
if(cols[v][i] == col)
id = i;
cols[v].erase(cols[v].begin() + id);
}
void move(int from, int to)
{
int v = poses[from].back();
poses[from].pop_back();
delCol(v, from);
poses[to].PB(v);
cols[v].PB(to);
}
void solve(int v)
{
//can be in one col
for(int col : cols[v])
{
}
}
void solve(VI vs)
{
FOR(i, 0, SZ(vs))
{
int j = i;
while(j < SZ(vs) && cnt[vs[j]] <= 0)
j++;
assert(j != SZ(vs));
RFOR(it, j + 1, i)
solve(vs[it]);
i = j;
while(j < SZ(vs) && cnt[vs[j]] >= 0)
j++;
FOR(it, i + 1, j)
solve(vs[it]);
i = j - 1;
}
}
void bad()
{
cout << "-1\n";
FOR(i, 0, n)
{
g[i].clear();
gr[i].clear();
cnt[i] = used[i] = 0;
}
ans.clear();
freeCol.clear();
}
void solve()
{
FOR(i, 0, m)
{
if(SZ(poses[i]) != 2)
continue;
int solvedFirst = poses[i][1];
int solvedSecond = poses[i][0];
cnt[solvedFirst]++;
cnt[solvedSecond]--;
g[solvedFirst].PB(solvedSecond);
gr[solvedSecond].PB(solvedFirst);
}
FOR(i, 0, n)
{
if(SZ(g[i]) + SZ(gr[i]) == 0)
{
solve(i);
used[i] = 1;
}
}
FOR(i, 0, n)
{
if(!used[i] && SZ(g[i]) + SZ(gr[i]) == 1)
{
component.clear();
dfs(i);
solve(component);
}
}
FOR(i, 0, n)
{
if(!used[i] && cnt[i] > 0)
{
component.clear();
dfs(i);
if(SZ(freeCol) == 0)
{
bad();
return;
}
component.erase(component.begin());
solve(i);
for(int to : g[i])
cnt[to]++;
solve(component);
}
}
FOR(i, 0, n)
{
if(!used[i])
{
assert(cnt[i] == 0);
component.clear();
dfs(i);
if(SZ(freeCol) == 0)
{
bad();
return;
}
component.erase(component.begin());
component.PB(i);
move(whereUp(i), freeCol.back());
cnt[i]--;
for(int to : g[i])
cnt[to]++;
solve(component);
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
while(cin >> n >> m)
{
FOR(i, 0, m)
{
int k;
cin >> k;
VI a(k);
FOR(j, 0, k)
{
cin >> a[j];
a[j]--;
}
if(k == 0)
freeCol.PB(i);
poses[i] = a;
}
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2 3 2 1 2 2 1 2 0 1 1 2 1 1 3 4 2 1 3 2 2 3 1 1 1 2