結果

問題 No.1615 Double Down
ユーザー heno239heno239
提出日時 2021-07-21 23:07:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,517 bytes
コンパイル時間 2,655 ms
コンパイル使用メモリ 170,360 KB
実行使用メモリ 16,176 KB
最終ジャッジ日時 2024-07-17 20:13:56
合計ジャッジ時間 13,716 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 5 ms
7,616 KB
testcase_46 AC 4 ms
7,492 KB
testcase_47 AC 5 ms
7,620 KB
testcase_48 AC 5 ms
7,492 KB
testcase_49 AC 4 ms
7,620 KB
testcase_50 AC 5 ms
7,492 KB
testcase_51 WA -
testcase_52 AC 5 ms
7,496 KB
testcase_53 AC 5 ms
7,492 KB
testcase_54 AC 4 ms
7,624 KB
testcase_55 WA -
testcase_56 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

struct edge { int to; ll cap; int rev; };
struct Dinic {
	int n;
	vector<vector<edge>> v;
	vector<int> dist, iter;
	Dinic(int sz) :n(sz), v(sz), dist(sz), iter(sz) {}

	void addedge(int from, int to, ll cap) {
		int x = v[to].size(), y = v[from].size();
		v[from].push_back({ to,cap,x });
		v[to].push_back({ from,0,y });
	}

	void bfs(int s) {
		fill(dist.begin(), dist.end(), -1);
		queue<int> q;
		dist[s] = 0;
		q.push(s);
		while (q.size()) {
			int x = q.front(); q.pop();
			rep(i, v[x].size()) {
				edge& e = v[x][i];
				if (e.cap > 0 && dist[e.to] < 0) {
					dist[e.to] = dist[x] + 1;
					q.push(e.to);
				}
			}
		}
	}

	ll dfs(int x, int t, ll f) {
		if (x == t)return f;
		for (int& i = iter[x]; i < (int)v[x].size(); ++i) {
			edge& e = v[x][i];
			if (e.cap > 0 && dist[x] < dist[e.to]) {
				ll d = dfs(e.to, t, min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					v[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}

	ll max_flow(int s, int t) {
		ll flow = 0;
		while (1) {
			bfs(s);
			if (dist[t] < 0)return flow;
			fill(iter.begin(), iter.end(), 0);
			ll f;
			while ((f = dfs(s, t, 1LL << 62)) > 0)flow += f;
		}
	}
};

struct uf {
private:
	vector<int> par, ran;
public:
	uf(int n) {
		par.resize(n, 0);
		ran.resize(n, 0);
		rep(i, n) {
			par[i] = i;
		}
	}
	int find(int x) {
		if (par[x] == x)return x;
		else return par[x] = find(par[x]);
	}
	void unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y)return;
		if (ran[x] < ran[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if (ran[x] == ran[y])ran[x]++;
		}
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
};

void solve() {
	int n, m, k, l; cin >> n >> m >> k >> l;
	vector<int> x(l), y(l), z(l);
	rep(i, l) {
		cin >> x[i] >> y[i] >> z[i];
		x[i]--; y[i]--;
	}

	ll ans = 0;
	vector<int> tran(n), tram(m);
	vector<int> capn(n), capm(m);
	rep(i, n)tran[i] = i,capn[i]=1;
	rep(i, m)tram[i] = i,capm[i]=1;

	for (int i = k; i >= 0; i--) {
		Dinic dc(n + m + 2);
		int s = n + m;
		int t = s + 1;
		rep(i, n)dc.addedge(s, i, capn[i]);
		rep(i, m)dc.addedge(i + n, t, capm[i]);

		vector<vector<int>> G(n + m);
		rep(j, l)if (z[j] == i) {
			int a = tran[x[j]];
			int b = tram[y[j]];
			dc.addedge(a, b + n, 1);
			G[a].push_back(b + n);
			G[b + n].push_back(a);
		}
		int f = dc.max_flow(s, t);
		ll cost = 1 << i;
		ans += f * cost;

		vector<bool> used(n + m);
		vector<int> ptran(n), ptram(m);
		int tmpn = 0;
		int tmpm = 0;
		rep(i, n + m)if (!used[i]) {
			used[i] = true;
			queue<int> q;
			q.push(i);
			vector<int> cur;
			while (!q.empty()) {
				int id = q.front(); q.pop();
				cur.push_back(id);
				for (int to : G[id])if (!used[to]) {
					used[to] = true; q.push(to);
				}
			}
			vector<int> idn, idm;
			for (int id : cur) {
				if (id < n)idn.push_back(id);
				else idm.push_back(id - n);
			}
			if (idn.size()) {
				for (int id : idn)ptran[id] = tmpn; tmpn++;
			}
			if (idm.size()) {
				for (int id : idm)ptram[id] = tmpm; tmpm++;
			}
		}
		vector<int> ncapn(n), ncapm(m);
		rep(i, n) {
			ncapn[ptran[i]] += capn[i];
		}
		rep(i, m) {
			ncapm[ptram[i]] += capm[i];
		}
		rep(i, n)for (edge e : dc.v[i])if (e.to != s && e.cap == 0) {
			int a = ptran[i];
			int b = ptram[e.to - n];
			ncapn[a]--;
			ncapm[b]--;
		}
		swap(tran, ptran);
		swap(tram, ptram);
		swap(capn, ncapn);
		swap(capm, ncapm);
	}
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(12);
	//init_f();
	//init();
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}

0