QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#132377 | #6329. Colorful Graph | Shuishui | WA | 2500ms | 8896kb | C++14 | 4.0kb | 2023-07-29 18:19:12 | 2023-07-29 18:19:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 7e3 + 2;
const int V = 1e5, E = 1e5;
template<typename T>
struct MinGraph {
int s, t, vtot;
int head[V], etot;
T dis[V], flow, cost, pf[V];
int pre[V];
bool vis[V];
struct edge{
int v, nxt;
T f, c;
} e[E * 2];
void addedge(int u, int v, T f, T c, T f2 = 0)
{
e[etot] = {v, head[u], f, c}; head[u] = etot++;
e[etot] = {u, head[v], f2, -c}; head[v] = etot++;
}
bool spfa()
{
T inf = numeric_limits<T>::max() / 2;
for (int i = 1; i <= vtot; i++)
{
dis[i] = inf;
vis[i] = false;
pre[i] = -1;
pf[i] = inf;
}
dis[s] = 0;
vis[s] = true;
queue<int> q;
q.push(s);
while (!q.empty())
{
int u = q.front();;
for (int i = head[u]; ~i; i = e[i].nxt)
{
int v = e[i].v;
if (e[i].f && dis[v] > dis[u] + e[i].c)
{
dis[v] = dis[u] + e[i].c;
pre[v] = i;
pf[v] = min(pf[u], e[i].f);
if (!vis[v])
{
vis[v] = 1;
q.push(v);
}
}
}
q.pop();
vis[u] = false;
}
return dis[t] != inf;
}
void augment()
{
int u = t;
T f = pf[t];
flow += f;
cost += f * dis[t];
while (~pre[u])
{
e[pre[u]].f -= f;
e[pre[u]^1].f += f;
u = e[pre[u]^1].v;
}
}
pair<T, T> solve()
{
flow = 0;
cost = 0;
while (spfa()) augment();
return {flow, cost};
}
void init(int s_, int t_, int vtot_)
{
s = s_, t = t_, vtot = vtot_;
etot = 0;
for (int i = 1; i <= vtot; i++) head[i] = -1;
}
};
int dfn[N], low[N], bel[N], sz[N], tot, cnt;
bool ins[N];
stack<int> stk;
vector<int> scc[N];
vector<int> e[N];
void dfs(int x)
{
dfn[x] = low[x] = ++tot;
ins[x] = true;
stk.push(x);
for (auto y : e[x])
{
if (!dfn[y]) dfs(y);
if (ins[y]) low[x] = min(low[x], low[y]);
}
if (dfn[x] == low[x])
{
vector<int> v;
cnt++;
while (true)
{
int y = stk.top();
stk.pop();
ins[y] = false;
scc[cnt].push_back(y);
bel[y] = cnt;
sz[cnt]++;
if (y == x) break;
}
}
}
MinGraph<ll> g;
void solve(void)
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
e[u].push_back(v);
}
for (int i = 1; i <= n; i++) if (!dfn[i]) dfs(i);
// for (int i = 1; i <= n; i++)
// cout << bel[i] << " ";
// cout << "\n";
// cout << cnt << "\n";
int s = cnt * 2 + 1, t = cnt * 2 + 2;
g.init(s, t, t);
vector<array<int, 2>> used;
for (int i = 1; i <= cnt; i++)
{
used.push_back({i, g.etot});
g.addedge(s, i * 2 - 1, m, 1);
g.addedge(i * 2 - 1, t, 1, 0);
g.addedge(s, i * 2, 1, 0);
g.addedge(i * 2 - 1, i * 2, m, 0);
}
for (int x = 1; x <= n; x++)
{
int i = bel[x];
for (auto y : e[x])
{
int u = bel[x], v = bel[y];
if (u == v)
continue;
// cout << u << " " << v << "\n";
g.addedge(u * 2, v * 2 - 1, m, 0);
}
}
vector<int> ans(n + 2);
auto [f, c] = g.solve();
// cout << c << "\n";
vector<int> vis(n * 2 + 2);
int now = 0;
for (auto [uid, eid] : used)
{
if (g.e[eid].f != m)
{
// cout << "\n";
// cout << uid << "\n";
now++;
vis[uid * 2] = true;
for (auto v : scc[uid])
{
// cout << v << "\n";
ans[v] = now;
}
queue<int> q;
q.push(uid * 2);
while (!q.empty())
{
int u = q.front();
// cout << u << "\n";
for (int i = g.head[u]; ~i; i = g.e[i].nxt)
{
int v = g.e[i].v;
if (v != t && v != s && g.e[i].f != m && !vis[v])
{
vis[v] = true;
// cout << v << "\n";
// cout << (v + 1) / 2 << "\n";
for (auto nd : scc[(v + 1) / 2])
ans[nd] = now;
q.push(v + 1);
}
}
q.pop();
}
}
}
for (int i = 1; i <= n; i++)
cout << ans[i] << " ";
cout << "\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// int T;
// cin >> T;
// for (int i = 1; i <= T; i++)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 7916kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
2 2 2 1 2
result:
ok AC
Test #2:
score: 0
Accepted
time: 2ms
memory: 7644kb
input:
5 7 1 2 2 1 4 3 5 1 5 4 4 1 4 5
output:
1 1 2 2 2
result:
ok AC
Test #3:
score: 0
Accepted
time: 2ms
memory: 7916kb
input:
8 6 6 1 3 4 3 6 2 3 4 1 6 4
output:
1 1 1 1 2 1 3 4
result:
ok AC
Test #4:
score: -100
Wrong Answer
time: 2500ms
memory: 8896kb
input:
7000 6999 4365 4296 2980 3141 6820 4995 4781 24 2416 5844 2940 2675 3293 2163 3853 5356 262 6706 1985 1497 5241 3803 353 1624 5838 4708 5452 3019 2029 6161 3849 4219 1095 1453 4268 4567 1184 1857 2911 3977 1662 2751 6353 6496 2002 6628 1407 4623 425 1331 4445 4277 1259 3165 4994 1044 2756 5788 5496 ...
output:
1 1 1 1 3 2 3 1 1 1 1 1 4 1 1 5 1 6 1 1 1 1 2 1 1 7 8 6 1 9 1 1 10 11 1 1 1 12 2 13 1 1 14 1 15 1 16 2 17 18 1 19 1 3 1 1 1 20 1 21 1 3 3 2 1 22 2 2 1 23 24 1 25 1 26 1 6 1 1 2 2 2 1 1 1 1 1 1 1 2 1 6 122 1 2 1 1 1 27 1 3 3 1 1 1 28 1 3 1 29 3 1 30 1 1 1 1 31 3 2 3 32 33 3 1 1 34 1 1 35 1 1 36 1 37 ...
result:
wrong answer 10 and 3 are not connected