Skip to content

Commit

Permalink
Codeforces Gym: 2003-2004 Winter Petrozavodsk Camp, Andrew Stankevich…
Browse files Browse the repository at this point in the history
… Contest 4 (ASC 4)
  • Loading branch information
Macesuted committed Feb 8, 2025
1 parent 8f1e22d commit c1a836f
Show file tree
Hide file tree
Showing 9 changed files with 888 additions and 0 deletions.
116 changes: 116 additions & 0 deletions Codeforces Gym/100198B.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* @file 100198B.cpp
* @author Macesuted ([email protected])
* @date 2025-01-11
*
* @copyright Copyright (c) 2025
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;

class Dinic {
private:
struct Edge {
int to, cap, cost, rev;
};

vector<vector<Edge>> graph;
vector<vector<Edge>::iterator> cur;
vector<bool> vis;
queue<int> que;
int n, S, T;

int dfs(int p, int rest) {
if (p == T) return rest;
vis[p] = true;
int use = 0, c;
for (auto i = cur[p]; i != graph[p].end() && rest; i++) {
cur[p] = i;
if (!i->cap || vis[i->to]) continue;
c = dfs(i->to, min(rest, i->cap));
i->cap -= c, graph[i->to][i->rev].cap += c, use += c, rest -= c;
}
vis[p] = false;
return use;
}

public:
void resize(int _n) { return graph.resize((n = _n) + 1), cur.resize(n + 1), vis.resize(n + 1); }
void addEdge(int from, int to, int cap, int cost) {
return graph[from].push_back(Edge{to, cap, cost, (int)graph[to].size()}),
graph[to].push_back(Edge{from, 0, -cost, (int)graph[from].size() - 1});
}
void maxFlow(int _S, int _T) {
S = _S, T = _T;

sort(graph[S].begin(), graph[S].end(), [](const Edge& a, const Edge& b) { return a.cost > b.cost; });

vis[S] = true;
for (auto& e : graph[S]) {
for (int i = 1; i <= n; i++) cur[i] = graph[i].begin();
if (dfs(e.to, 1)) e.cap = 0, graph[e.to][e.rev].cap = 1;
}

return;
}
vector<int> getAns(int n) {
vector<int> ans(n + 1);
for (int i = 1; i <= n; i++)
for (auto e : graph[i])
if (e.to != S && !e.cap) ans[i] = e.to - n;
return ans;
}
};

void solve(void) {
int n;
cin >> n;

int S = 2 * n + 1, T = 2 * n + 2;
Dinic dnc;
dnc.resize(T);

for (int i = 1, v; i <= n; i++) cin >> v, dnc.addEdge(S, i, 1, v * v), dnc.addEdge(n + i, T, 1, 0);

for (int i = 1, s, x; i <= n; i++) {
cin >> s;
while (s--) cin >> x, dnc.addEdge(i, n + x, 1, 0);
}

dnc.maxFlow(S, T);

auto ret = dnc.getAns(n);

for (int i = 1; i <= n; i++) cout << ret[i] << ' ';
cout << endl;

return;
}

bool mem2;

int main() {
#ifndef LOCAL
freopen("beloved.in", "r", stdin), freopen("beloved.out", "w", stdout);
#endif
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif

int _ = 1;
while (_--) solve();

#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}
75 changes: 75 additions & 0 deletions Codeforces Gym/100198E.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @file 100198E.cpp
* @author Macesuted ([email protected])
* @date 2025-01-11
*
* @copyright Copyright (c) 2025
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;

#define maxn 405

typedef pair<int, int> pii;

vector<pii> graph[maxn];
vector<int> pos[maxn];
int dis[maxn];

void solve(void) {
int n, m, S, T;
cin >> n >> m >> S >> T;

for (int i = 1, x, y; i <= m; i++) cin >> x >> y, graph[x].emplace_back(y, i), graph[y].emplace_back(x, i);

queue<int> que;
que.push(S), dis[S] = 1;
while (!que.empty()) {
int p = que.front();
que.pop();
for (auto [q, id] : graph[p])
if (!dis[q]) dis[q] = dis[p] + 1, que.push(q);
}

for (int i = 1; i <= n; i++)
for (auto [j, id] : graph[i])
if (dis[i] < dis[j]) pos[dis[j]].push_back(id);

cout << dis[T] - 1 << endl;

for (int i = 2; i <= dis[T]; i++) {
cout << pos[i].size() << ' ';
for (auto id : pos[i]) cout << ' ' << id;
cout << endl;
}

return;
}

bool mem2;

int main() {
#ifndef LOCAL
freopen("defence.in", "r", stdin), freopen("defence.out", "w", stdout);
#endif
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif

int _ = 1;
while (_--) solve();

#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}
130 changes: 130 additions & 0 deletions Codeforces Gym/100200A.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* @file 100200A.cpp
* @author Macesuted ([email protected])
* @date 2025-02-08
*
* @copyright Copyright (c) 2025
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;
class Dinic {
private:
struct Edge {
int to, cap, rev;
bool mark;
};

vector<vector<Edge>> graph;
vector<vector<Edge>::iterator> cur;
vector<int> dist, rdist;
queue<int> que;
int n, S, T;

bool bfs(void) {
for (int i = 1; i <= n; i++) dist[i] = INT_MAX, cur[i] = graph[i].begin();
que.push(S), dist[S] = 0;
while (!que.empty()) {
int p = que.front();
que.pop();
for (auto i : graph[p])
if (i.cap && dist[i.to] > dist[p] + 1) dist[i.to] = dist[p] + 1, que.push(i.to);
}
return dist[T] != INT_MAX;
}
void rbfs(void) {
for (int i = 1; i <= n; i++) rdist[i] = INT_MAX;
que.push(T), rdist[T] = 0;
while (!que.empty()) {
int p = que.front();
que.pop();
for (auto i : graph[p])
if (graph[i.to][i.rev].cap && rdist[i.to] > rdist[p] + 1) rdist[i.to] = rdist[p] + 1, que.push(i.to);
}
return;
}
int64_t dfs(int p, int64_t rest) {
if (p == T) return rest;
int64_t use = 0, c;
for (auto i = cur[p]; i != graph[p].end() && rest; i++) {
cur[p] = i;
if (!i->cap || dist[i->to] != dist[p] + 1) continue;
if (!(c = dfs(i->to, min(rest, (int64_t)i->cap)))) dist[i->to] = -1;
i->cap -= c, graph[i->to][i->rev].cap += c, use += c, rest -= c;
}
return use;
}

public:
void resize(int _n) { return graph.resize((n = _n) + 1), cur.resize(n + 1), dist.resize(n + 1), rdist.resize(n + 1); }
void addEdge(int from, int to, int cap) {
return graph[from].push_back(Edge{to, cap, (int)graph[to].size(), false}),
graph[to].push_back(Edge{from, cap, (int)graph[from].size() - 1, false});
}
int64_t maxFlow(int _S, int _T) {
S = _S, T = _T;
int64_t ans = 0;
while (bfs()) ans += dfs(S, INT64_MAX);
return ans;
}
bool solve(void) {
bfs(), rbfs();
for (int p = 1; p <= n; p++)
for (auto &e : graph[p]) {
if (!e.cap && dist[p] != INT_MAX && rdist[e.to] != INT_MAX) e.mark = true;
}

for (int i = 1; i <= n; i++) dist[i] = 0;
que.push(S);
while (!que.empty()) {
int p = que.front();
que.pop();
for (auto i : graph[p])
if (!i.mark && !dist[i.to]) dist[i.to] = 1, que.push(i.to);
}

return dist[T];
}
};

void solve(void) {
int n, m, S, T;
cin >> n >> m >> S >> T;

Dinic dnc;
dnc.resize(n);
for (int i = 1, x, y, f; i <= m; i++) cin >> x >> y >> f, dnc.addEdge(x, y, f);

dnc.maxFlow(S, T);

cout << (dnc.solve() ? "AMBIGUOUS" : "UNIQUE") << endl;

return;
}

bool mem2;

int main() {
#ifndef LOCAL
freopen("attack.in", "r", stdin), freopen("attack.out", "w", stdout);
#endif
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif

int _ = 1;
while (_--) solve();

#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}
Loading

0 comments on commit c1a836f

Please sign in to comment.