text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, head[200010], num, from, to, T;
int read() {
int x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x;
}
struct xx {
int next, to;
} way[1000010];
void add(int from, int to) {
way[++num].next = head[from];
way[num].to = to;
head[from] = num;
}
int dfn[200010], s, c[200010], size[200010], t, o, low[200010];
bool vis[200010];
void dfs(int x, int fa, int d) {
size[d]++;
c[x] = d;
dfn[x] = low[x] = ++t;
for (int i = head[x]; i; i = way[i].next) {
int y = way[i].to;
if (y == fa) continue;
if (!dfn[y]) {
dfs(y, x, d);
} else
low[x] = min(low[x], dfn[y]);
}
}
int si, minl;
void search(int x, int fa) {
vis[x] = 1;
si++;
minl = min(minl, low[x]);
for (int i = head[x]; i; i = way[i].next) {
int y = way[i].to;
if (y == fa) continue;
if (dfn[y] < dfn[x]) continue;
if (!vis[y]) {
search(y, x);
}
}
}
int main() {
T = read();
while (T--) {
n = read();
m = read();
x = read();
y = read();
for (int i = 1; i <= n; ++i) head[i] = 0;
num = 0;
for (int i = 1; i <= m; ++i) {
from = read();
to = read();
add(from, to);
add(to, from);
}
dfn[x] = 1;
t = 1;
int co = 0;
o = y;
for (int i = head[x]; i; i = way[i].next) {
int y = way[i].to;
if (dfn[y]) continue;
co++;
dfs(y, x, co);
}
int gg = n - 1 - size[c[y]];
long long ans = 0;
for (int i = head[y]; i; i = way[i].next) {
int g = way[i].to;
if (dfn[g] < dfn[y]) continue;
if (vis[g]) continue;
si = 0;
minl = n + 10;
search(g, y);
if (minl >= dfn[y]) ans += 1ll * gg * si;
}
printf("%lld\n", ans);
for (int i = 1; i <= co; ++i) size[i] = 0;
for (int i = 1; i <= n; ++i) dfn[i] = low[i] = vis[i] = 0;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int INF = 3000000000000000000;
void dfs(vector<long long int> adj[], bool vis[], long long int n,
long long int l) {
vis[n] = true;
for (auto x : adj[n]) {
if (x != l && n != l) {
if (!vis[x]) {
dfs(adj, vis, x, l);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1, xp = 1;
cin >> t;
while (t--) {
long long int i = 0, h, j = 0, x = 0, y = 0, n, m, k = 0, l = 0, r = 0,
o = 0, cnt1 = INT_MAX, cnt2 = 0, cnt3 = INT_MIN, cnt4 = 0,
min1 = INT_MIN;
cin >> n >> m >> l >> r;
vector<long long int> adj[n];
bool vis[n];
for (i = 0; i < m; i++) {
cin >> x >> y;
x--;
y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
l--;
r--;
memset(vis, false, sizeof(vis));
vis[l] = true;
vis[r] = true;
dfs(adj, vis, l, r);
bool vis1[n];
memset(vis1, false, sizeof(vis1));
vis1[l] = true;
vis1[r] = true;
dfs(adj, vis1, r, l);
for (i = 0; i < n; i++) {
if (!vis[i]) cnt2++;
if (!vis1[i]) cnt4++;
}
cout << cnt2 * cnt4 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200100;
vector<int> e[maxn], component[maxn];
bool vis[maxn];
int cnt = 0, target, place, num;
void dfs(int x) {
component[cnt].push_back(x);
vis[x] = 1;
if (x == target) place = cnt;
for (auto k : e[x]) {
if (!vis[k]) dfs(k);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
int n, m, a, b;
cin >> n >> m >> a >> b;
for (int i = 0; i <= n; i++) {
e[i].clear();
component[i].clear();
}
cnt = 0;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
vis[a] = 1;
target = b;
for (auto k : e[a]) {
if (!vis[k]) dfs(k);
cnt++;
}
long long ans1 = n - 1 - component[place].size();
cnt = 0;
memset(vis, 0, sizeof(vis));
for (int i = 0; i <= n; i++) component[i].clear();
swap(a, b);
vis[a] = 1;
target = b;
for (auto k : e[a]) {
if (!vis[k]) dfs(k);
cnt++;
}
long long ans2 = n - 1 - component[place].size();
cout << ans1 * ans2 << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void bfs(long long int start, vector<vector<long long int>>& adj,
vector<long long int>& vis) {
vis[start] = 1;
queue<long long int> bfs;
bfs.push(start);
while (!bfs.empty()) {
long long int cur = bfs.front();
bfs.pop();
for (auto x : adj[cur]) {
if (!vis[x]) {
vis[x] = 1;
bfs.push(x);
}
}
}
}
void solve() {
long long int n, m, a, b;
cin >> n >> m >> a >> b;
vector<vector<long long int>> adj(n);
vector<vector<long long int>> adj2(n);
vector<long long int> wt(n);
vector<long long int> vis(n);
vector<long long int> vis2(n);
a--, b--;
for (long long int i = 0; i < m; i++) {
long long int x, y;
cin >> x >> y;
x--, y--;
if (x != a && y != a) {
adj[x].push_back(y);
adj[y].push_back(x);
}
if (x != b && y != b) {
adj2[x].push_back(y);
adj2[y].push_back(x);
}
}
bfs(b, adj, vis);
bfs(a, adj2, vis2);
map<long long int, long long int> rep;
for (long long int i = 0; i < n; i++) {
if (vis[i] && vis2[i]) rep[i] = 1;
}
long long int a1 = 0, a2 = 0;
for (long long int i = 0; i < n; i++) {
if (vis[i] && i != b && rep[i] == 0) a1++;
if (vis2[i] && i != a && rep[i] == 0) a2++;
}
cout << a1 * a2 << endl;
}
signed main() {
long long int t;
cin >> t;
while (t--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int a, b;
int vis1[200005], vis2[200005];
vector<int> arr[200005];
void ini(int n, int vis1[], int vis2[]) {
for (int i = 0; i <= n; i++) {
vis1[i] = 0, vis2[i] = 0;
arr[i].clear();
}
}
void dfs1(int s, int vis1[]) {
vis1[s] = 1;
for (int i = 0; i < arr[s].size(); i++) {
if (arr[s][i] != b && vis1[arr[s][i]] == 0) dfs1(arr[s][i], vis1);
}
}
void dfs2(int s, int vis2[]) {
vis2[s] = 1;
for (int i = 0; i < arr[s].size(); i++) {
if (arr[s][i] != a && vis2[arr[s][i]] == 0) dfs2(arr[s][i], vis2);
}
}
int main() {
int tt = 1;
scanf("%d", &tt);
;
while (tt--) {
int i, n, m, x, y;
unsigned long long int ans1 = 0, ans2 = 0;
scanf("%d %d", &n, &m);
;
scanf("%d %d", &a, &b);
;
for (int i = 0; i < m; i++) {
scanf("%d %d", &x, &y);
;
arr[x].push_back(y);
arr[y].push_back(x);
}
dfs1(a, vis1);
dfs2(b, vis2);
for (int i = 1; i <= n; i++) {
if (vis2[i] == 0 && vis1[i] == 1 && i != a && i != b)
ans1++;
else if (vis2[i] == 1 && vis1[i] == 0 && i != a && i != b)
ans2++;
}
unsigned long long int ans = ans1 * ans2;
printf("%lli\n", ans);
ini(n + 1, vis1, vis2);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int M = 2e5 + 1;
vector<vector<long long int>> g(M);
vector<bool> vis(M);
vector<long long int> s(M);
void dfs(long long int u) {
vis[u] = true;
for (auto x : g[u]) {
if (vis[x]) continue;
dfs(x);
}
}
void dfs1(long long int u) {
s[u] = 1;
vis[u] = true;
for (auto x : g[u]) {
if (vis[x]) continue;
dfs1(x);
s[u] += s[x];
}
}
void solve() {
long long int n, m, a, b;
cin >> n >> m >> a >> b;
for (long long int i = 0; i < m; i++) {
long long int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<long long int> v(n + 1, 0);
vis[a] = true;
dfs(b);
bool fl1 = true;
for (long long int i = 1; i <= n; i++) {
v[i] += (vis[i]);
if (vis[i] == false) {
fl1 = false;
}
vis[i] = false;
}
vis[b] = true;
dfs(a);
bool fl2 = true;
for (long long int i = 1; i <= n; i++) {
v[i] += (vis[i]);
if (vis[i] == false) {
fl2 = false;
}
vis[i] = false;
}
if (fl1 || fl2) {
cout << 0 << '\n';
;
} else {
for (long long int i = 1; i <= n; i++) {
vis[i] = (v[i] == 2);
}
dfs1(a);
long long int sa = s[a];
for (long long int i = 1; i <= n; i++) {
s[i] = 0;
vis[i] = (v[i] == 2);
}
dfs1(b);
long long int sb = s[b];
cout << (sa - 1) * (sb - 1) << '\n';
;
}
for (long long int i = 1; i <= n; i++) {
vis[i] = false;
s[i] = 0;
g[i].clear();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tc;
tc = 1;
cin >> tc;
for (int i = 1; i <= tc; i++) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b;
unordered_set<int> arr1, arr2;
vector<int> arr[200005];
bool visited[200005];
void bfs1(int x) {
if (visited[x]) return;
visited[x] = 1;
for (int i : arr[x]) {
if (i == b || visited[i]) continue;
arr1.insert(i);
bfs1(i);
}
}
void bfs2(int x) {
if (visited[x]) return;
visited[x] = 1;
for (int i : arr[x]) {
if (visited[i] || i == a) continue;
if (arr1.find(i) != arr1.end())
arr1.erase(i);
else {
arr2.insert(i);
}
bfs2(i);
}
}
int main() {
int t;
cin >> t;
while (t--) {
cin >> n >> m >> a >> b;
memset(visited, 0, sizeof(visited));
arr1.clear();
arr2.clear();
for (int i = 0; i <= n; i++) arr[i].clear();
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
arr[x].push_back(y);
arr[y].push_back(x);
}
memset(visited, 0, sizeof(visited));
bfs1(a);
memset(visited, 0, sizeof(visited));
bfs2(b);
long long aa = arr1.size();
long long bb = arr2.size();
cout << aa * bb << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 2e18;
const long long mod = 1e9 + 7;
const long double pi = 3.141592653589793238462643383279502884;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void print(long long a[], long long n) {
for (long long i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << "\n";
}
long long power(long long x, long long y) {
if (y <= 0) return 1;
long long ans = 1;
x %= mod;
while (y) {
if (y & 1) ans = (x * ans) % mod;
x = (x * x) % mod;
y >>= 1;
}
return ans;
}
long long modInverse(long long n) { return power(n, mod - 2); }
inline long long mul(long long a, long long b) { return (a * b) % mod; }
inline long long sub(long long a, long long b) {
long long c = (a - b);
if (c < 0) c += mod;
return c;
}
inline long long add(long long a, long long b) {
long long c = (a + b);
if (c >= mod) c -= mod;
return c;
}
inline long long divi(long long a, long long b) {
return mul(a, modInverse(b));
}
const long long N = 2e5 + 1;
vector<long long> adj[N];
bool visited1[N];
bool visited2[N];
void dfs1(long long st, long long avoid) {
visited1[st] = 1;
for (auto u : adj[st]) {
if (u != avoid && !visited1[u]) dfs1(u, avoid);
}
}
void dfs2(long long st, long long avoid) {
visited2[st] = 1;
for (auto u : adj[st]) {
if (u != avoid && !visited2[u]) dfs2(u, avoid);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << fixed << setprecision(10);
int t;
cin >> t;
while (t--) {
long long n, m, a, b;
cin >> n >> m >> a >> b;
for (int i = 0; i < m; i++) {
long long u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs1(a, b);
dfs2(b, a);
long long c1 = -1, c2 = -1;
for (int i = 1; i <= n; i++) {
if (visited1[i] == 1 && visited2[i] == 0) c1++;
if (visited1[i] == 0 && visited2[i] == 1) c2++;
visited2[i] = 0;
visited1[i] = 0;
adj[i].clear();
}
cout << c1 * c2 << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int t, n, m, a, b, x, y;
vector<int> adj[200001];
int vis[200001];
void dfs(int src, int d) {
vis[src] = t + 1;
for (int ch : adj[src]) {
if ((d && ch == a) || (!d && ch == b) || vis[ch] == t + 1) {
continue;
}
dfs(ch, d);
}
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d%d%d", &n, &m, &a, &b);
while (m--) {
scanf("%d%d", &x, &y);
adj[x].emplace_back(y);
adj[y].emplace_back(x);
}
dfs(a, 0);
int cnt1 = 0, cnt2 = 0;
for (int i = 1; i <= n; ++i) {
if (vis[i] != t + 1 && i != a && i != b) {
cnt1++;
}
vis[i] = 0;
}
dfs(b, 1);
for (int i = 1; i <= n; ++i) {
if (vis[i] != t + 1 && i != a && i != b) {
cnt2++;
}
adj[i].clear();
}
printf("%lld\n", 1LL * cnt1 * cnt2);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int q, num;
vector<int> v[maxn];
bool vis[maxn];
void dfs(int x) {
for (int i = 0; i < v[x].size(); i++) {
if (!vis[v[x][i]]) {
num++;
vis[v[x][i]] = 1;
dfs(v[x][i]);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin >> q;
while (q--) {
int n, m, a, b;
long long ans;
cin >> n >> m >> a >> b;
for (int i = 1; i <= n; i++) {
v[i].clear();
vis[i] = 0;
}
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
vis[b] = vis[a] = 1, num = 0;
dfs(a);
ans = n - 2 - num;
fill(vis + 1, vis + 1 + n, 0);
vis[b] = vis[a] = 1, num = 0;
dfs(b);
ans *= (n - 2) - num;
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200005;
int visited_a[MAX_N];
int visited_b[MAX_N];
vector<int> route[MAX_N];
int n, m, a, b;
int main() {
int test_case_num = 0;
cin >> test_case_num;
for (int test_case = 1; test_case <= test_case_num; test_case++) {
cin >> n >> m >> a >> b;
unordered_set<int> a_neighbor;
unordered_set<int> b_neighbor;
unordered_set<int> ab_neighbor;
for (int i = 0; i <= n; i++) {
route[i].clear();
}
for (int i = 0; i < m; i++) {
int temp1, temp2;
cin >> temp1 >> temp2;
route[temp1].push_back(temp2);
route[temp2].push_back(temp1);
}
{
queue<int> q;
q.push(a);
visited_a[a] = test_case;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (vector<int>::iterator it = route[cur].begin();
it != route[cur].end(); it++) {
if (*it != b && visited_a[*it] != test_case) {
visited_a[*it] = test_case;
q.push(*it);
a_neighbor.insert(*it);
}
}
}
}
{
queue<int> q;
q.push(b);
visited_b[b] = test_case;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (vector<int>::iterator it = route[cur].begin();
it != route[cur].end(); it++) {
if (*it != a && visited_b[*it] != test_case) {
visited_b[*it] = test_case;
q.push(*it);
b_neighbor.insert(*it);
}
}
}
}
for (unordered_set<int>::iterator it = a_neighbor.begin();
it != a_neighbor.end(); it++) {
if (b_neighbor.find(*it) != b_neighbor.end()) {
ab_neighbor.insert(*it);
}
}
cout << (unsigned long long)((unsigned long long)a_neighbor.size() -
(unsigned long long)ab_neighbor.size()) *
((unsigned long long)b_neighbor.size() -
(unsigned long long)ab_neighbor.size())
<< endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> edge[300005];
int vis[300005];
bool flag = false;
int cnt = 0;
void dfs(int u, int x, int y) {
if (u == y) flag = true;
if (u == x || vis[u]) return;
vis[u] = 1;
cnt++;
for (int v : edge[u])
if (!vis[v] && v != x) dfs(v, x, y);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, m, x, y;
scanf("%d %d %d %d", &n, &m, &x, &y);
for (int i = 1; i <= n; i++) edge[i].clear();
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
}
int first, second;
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
cnt = 0;
flag = false;
dfs(i, x, y);
if (flag) {
first = n - cnt - 1;
break;
}
}
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
cnt = 0;
flag = false;
dfs(i, y, x);
if (flag) {
second = n - cnt - 1;
break;
}
}
printf("%lld\n", 1ll * first * second);
}
}
|
#include <bits/stdc++.h>
using namespace std;
char buf[1 << 20], *_ = buf, *__ = buf;
template <class T>
inline bool read(T &x) {
x = 0;
char c = (_ == __ && (__ = (_ = buf) + fread(buf, 1, 1 << 20, stdin), _ == __)
? EOF
: *_++);
bool f = 0;
while (c < 48 || c > 57) {
if (c == EOF) return 0;
f ^= (c == '-'),
c = (_ == __ &&
(__ = (_ = buf) + fread(buf, 1, 1 << 20, stdin), _ == __)
? EOF
: *_++);
}
while (47 < c && c < 58)
x = (x << 3) + (x << 1) + (c ^ 48),
c = (_ == __ && (__ = (_ = buf) + fread(buf, 1, 1 << 20, stdin), _ == __)
? EOF
: *_++);
if (f) x = -x;
return 1;
}
template <class T>
inline bool read(T &a, T &b) {
return read(a) && read(b);
}
const long long MAXN = 2e5 + 8;
vector<int> mp[MAXN];
int n, m, a, b;
int color[MAXN], vis[MAXN];
int e;
void dfs(int x, int col) {
if (x == e) return;
color[x] += col;
vis[x] = col;
for (int y : mp[x])
if (vis[y] ^ col) dfs(y, col);
}
int main() {
int t;
read(t);
while (t--) {
read(n, m), read(a, b);
for (int i = 0; i <= n; ++i) color[i] = 0, vis[i] = 0, mp[i].clear();
for (int i = 0, x, y; i < m; ++i) {
read(x, y);
mp[x].emplace_back(y);
mp[y].emplace_back(x);
}
e = b, dfs(a, 1);
e = a, dfs(b, 2);
long long x = 0, y = 0;
for (int i = 1; i <= n; ++i) {
if (color[i] == 1) x++;
if (color[i] == 2) y++;
}
printf("%I64d\n", (x - 1) * (y - 1));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> g[200005];
int vis[200005];
long long int v;
void dfs(int s, int t) {
v++;
vis[s]++;
for (auto x : g[s]) {
if (vis[x] == 0 and x != t) dfs(x, t);
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n, m, a, b;
cin >> n >> m;
cin >> a >> b;
for (int i = 0; i <= n; i++) {
g[i].clear();
vis[i] = 0;
}
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
long long x, y;
v = 0;
dfs(a, b);
x = v;
x = n - x - 1;
v = 0;
for (int i = 0; i <= n; i++) {
vis[i] = 0;
}
dfs(b, a);
y = v;
y = n - y - 1;
cout << x * y << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
ifstream f("1.in");
int n, m, a, b, t;
int da[200050], db[200050];
bool usedb[200050], useda[200050];
vector<int> v[200050];
long long ans[3];
void dfsa(int x) {
useda[x] = 1;
ans[1]++;
for (auto y : v[x])
if (!useda[y] && y != b) dfsa(y);
}
void dfsb(int x) {
usedb[x] = 1;
ans[2]++;
for (auto y : v[x])
if (!usedb[y] && y != a) dfsb(y);
}
int main() {
cin >> t;
while (t--) {
ans[1] = ans[2] = 0;
cin >> n >> m >> a >> b;
for (int i = 1; i <= n; i++) useda[i] = usedb[i] = 0;
for (int i = 1; i <= n; i++) v[i].clear();
int x, y;
while (m--) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfsa(a);
dfsb(b);
cout << (n - ans[1] - 1) * (n - ans[2] - 1) << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
vector<unsigned long long> p;
vector<unsigned long long> r;
vector<unsigned long long> c;
unsigned long long cnt = 0;
void MakeSet(unsigned long long x) {
p[x] = x;
r[x] = 0;
c[x] = 1;
}
unsigned int Find(unsigned long long x) {
return (x == p[x] ? x : p[x] = Find(p[x]));
}
void Union(unsigned long long x, unsigned long long y) {
if ((x = Find(x)) == (y = Find(y))) return;
if (r[x] < r[y]) {
p[x] = y;
c[y] += c[x];
--cnt;
} else {
p[y] = x;
c[x] += c[y];
--cnt;
if (r[x] == r[y]) ++r[x];
}
}
void solve() {
unsigned long long n, m, a, b;
cin >> n >> m >> a >> b;
cnt = n - 2;
p = vector<unsigned long long>(n + 1);
r = vector<unsigned long long>(n + 1);
c = vector<unsigned long long>(n + 1);
for (unsigned long long i = (0); i < (n); i++) {
MakeSet(i + 1);
}
unordered_map<unsigned long long, unordered_set<unsigned long long>> g;
for (unsigned long long i = (0); i < (m); i++) {
unsigned long long v1, v2;
cin >> v1 >> v2;
if (v1 == a && v2 != b)
g[a].insert(v2);
else if (v1 == b && v2 != a)
g[b].insert(v2);
if (v2 == a && v1 != b)
g[a].insert(v1);
else if (v2 == b && v1 != a) {
g[b].insert(v1);
}
if (v1 == a || v1 == b || v2 == a || v2 == b) continue;
Union(v1, v2);
}
if (cnt == 1) {
cout << 0 << endl;
return;
}
unordered_map<unsigned long long, pair<bool, bool>> flag;
for (const auto& v : g[a]) {
flag[Find(v)].first = true;
}
for (const auto& v : g[b]) {
flag[Find(v)].second = true;
}
unsigned long long cnt_a = 0, cnt_b = 0;
for (const auto& [v, has] : flag) {
const auto& [has_a, has_b] = has;
if (has_a && !has_b)
cnt_a += c[v];
else if (has_b && !has_a)
cnt_b += c[v];
}
cout << cnt_a * cnt_b << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
unsigned long long n;
cin >> n;
while (n-- > 0) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 200005;
long long n, m, a, b;
std::vector<long long> v[MAXN];
bool vis[MAXN];
long long novis;
long long cnt1, cnt2;
void dfs(long long p, bool now) {
vis[p] = true;
if (now)
cnt1--;
else
cnt2--;
for (long long i = 0; i < v[p].size(); i++) {
long long u = v[p][i];
if (!vis[u] && u != novis) {
dfs(u, now);
}
}
}
signed main() {
ios::sync_with_stdio(false);
long long t;
cin >> t;
while (t--) {
cin >> n >> m >> a >> b;
cnt1 = cnt2 = n;
for (long long i = 1; i <= n; i++) {
v[i].clear();
vis[i] = false;
}
for (long long i = 1; i <= m; i++) {
long long x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
novis = a;
dfs(b, true);
for (long long i = 1; i <= n; i++) vis[i] = false;
novis = b;
dfs(a, false);
cnt1--;
cnt2--;
cout << cnt1 * cnt2 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
namespace cplib {
using namespace std;
template <typename InputIt,
typename T = typename iterator_traits<InputIt>::value_type>
void read_n(InputIt it, int n) {
copy_n(istream_iterator<T>(cin), n, it);
}
template <typename InputIt,
typename T = typename iterator_traits<InputIt>::value_type>
void read(InputIt first, InputIt last) {
read_n(first, distance(first, last));
}
template <typename InputIt,
typename T = typename iterator_traits<InputIt>::value_type>
void write(InputIt first, InputIt last, const char *delim = "\n") {
copy(first, last, ostream_iterator<T>(cout, delim));
}
} // namespace cplib
using namespace std;
using namespace cplib;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void) {
ios::sync_with_stdio(false), cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, m, a, b;
cin >> n >> m >> a >> b, a--, b--;
vector<vi> g(n);
while (m--) {
int u, v;
cin >> u >> v, u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
vector<bool> vis(n, false);
function<int(int, int)> dfs = [&](int u, int exc) {
int cnt = 1;
vis[u] = true;
for (auto v : g[u])
if (!vis[v] and v != exc) cnt += dfs(v, exc);
return cnt;
};
ll x = dfs(a, b);
fill(begin(vis), end(vis), false);
ll y = dfs(b, a);
cout << (n - x - 1) * (n - y - 1) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> edge[300005];
int vis[300005];
bool flag = false;
int cnt = 0;
void dfs(int u, int x, int y) {
if (u == y) flag = true;
if (u == x || vis[u]) return;
vis[u] = 1;
cnt++;
for (int v : edge[u])
if (!vis[v] && v != x) dfs(v, x, y);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, m, x, y;
scanf("%d %d %d %d", &n, &m, &x, &y);
for (int i = 1; i <= n; i++) edge[i].clear();
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
}
int first, second;
for (int i = 1; i <= n; i++) vis[i] = 0;
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
cnt = 0;
flag = false;
dfs(i, x, y);
if (flag) {
first = n - cnt - 1;
break;
}
}
for (int i = 1; i <= n; i++) vis[i] = 0;
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
cnt = 0;
flag = false;
dfs(i, y, x);
if (flag) {
second = n - cnt - 1;
break;
}
}
printf("%lld\n", 1ll * first * second);
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
bool vis[maxn][2];
vector<int> G[maxn];
int n, m, a, b;
void dfs(int pos, int type) {
for (int i = 0; i < G[pos].size(); i++) {
int v = G[pos][i];
if (vis[v][type]) continue;
if (v != a && v != b) {
vis[v][type] = true;
dfs(v, type);
}
}
}
int main() {
int t, u, v;
cin >> t;
while (t--) {
memset(vis, false, sizeof(vis));
cin >> n >> m >> a >> b;
for (int i = 1; i <= n; i++) G[i].clear();
for (int i = 0; i < m; i++) {
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(a, 0);
dfs(b, 1);
long long first = 0;
long long second = 0;
for (int i = 1; i <= n; i++) {
if (vis[i][0] && !vis[i][1]) first++;
if (!vis[i][0] && vis[i][1]) second++;
}
cout << first * second << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
ifstream in("input.in");
ofstream out("output.out");
const long long nmx = 2e5 + 5;
const long long MOD = 1e9 + 7;
long long n, m, a, b, cnt[nmx] = {};
bitset<nmx> vis;
vector<long long> li[nmx];
queue<long long> q;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
cin >> n >> m >> a >> b;
for (int i = 1; i <= n; ++i) {
li[i].clear();
cnt[i] = 0;
}
vis.reset();
for (int i = 1; i <= m; ++i) {
long long x, y;
cin >> x >> y;
li[x].push_back(y);
li[y].push_back(x);
}
q.push(a);
while (!q.empty()) {
long long f = q.front();
q.pop();
for (auto k : li[f]) {
if (vis[k] == 0 && k != b && k != a) {
++cnt[k];
vis[k] = 1;
q.push(k);
}
}
}
vis.reset();
q.push(b);
while (!q.empty()) {
long long f = q.front();
q.pop();
for (auto k : li[f]) {
if (vis[k] == 0 && k != a && k != b) {
++cnt[k];
vis[k] = 1;
q.push(k);
}
}
}
long long fromA = 0;
q.push(a);
vis.reset();
while (!q.empty()) {
long long f = q.front();
q.pop();
for (auto k : li[f]) {
if (vis[k] == 0 && cnt[k] == 1) {
vis[k] = 1;
q.push(k);
++fromA;
}
}
}
long long fromB = 0;
q.push(b);
vis.reset();
while (!q.empty()) {
long long f = q.front();
q.pop();
for (auto k : li[f]) {
if (vis[k] == 0 && cnt[k] == 1) {
vis[k] = 1;
q.push(k);
++fromB;
}
}
}
cout << fromA * fromB << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
static char now[1 << 16], *S, *T;
if (T == S) {
T = (S = now) + fread(now, 1, 1 << 16, stdin);
if (T == S) return EOF;
}
return *S++;
}
inline long long read() {
long long x = 0, f = 1;
char ch = gc();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = gc();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = gc();
return x * f;
}
struct edge {
long long to, nxt;
} g[1000001];
long long head[1000001], sum, N, n, m, A, B, tot, id, low[1000001],
dfn[1000001];
stack<long long> S;
inline void made(long long from, long long to) {
g[++tot].to = to;
g[tot].nxt = head[from];
head[from] = tot;
}
namespace RST {
struct edge {
long long to, nxt;
} g[1000001];
long long head[1000001], sz[1000001], a[1000001], tot;
long long ANS;
inline void made(long long from, long long to) {
g[++tot].to = to;
g[tot].nxt = head[from];
head[from] = tot;
}
bool dfs(long long u, long long fa) {
sz[u] = (u <= n);
long long xxxx = 0;
bool fff = (u == B);
for (long long i = head[u]; i; i = g[i].nxt) {
long long v = g[i].to;
if (v == fa) continue;
bool xxx = dfs(v, u);
sz[u] += sz[v];
if (xxx) xxxx = sz[v], fff = 1;
}
if (u == A) {
ANS = 1ll * (sz[B] - 1) * (sz[A] - xxxx - 1);
}
return fff;
}
} // namespace RST
inline void mading(long long u, long long v) {
RST::made(u, v);
RST::made(v, u);
}
void tarjan(long long u, long long fa) {
dfn[u] = low[u] = ++id;
S.push(u);
RST::a[u] = -1;
N++;
for (long long i = head[u]; i; i = g[i].nxt) {
long long v = g[i].to;
if (!dfn[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
sum++;
long long x;
do {
x = S.top();
S.pop();
mading(sum, x);
RST::a[sum]++;
} while (x != v);
mading(sum, u);
RST::a[sum]++;
}
}
low[u] = min(low[u], dfn[v]);
}
}
signed main() {
long long T = read();
while (T--) {
n = read(), m = read(), A = read(), B = read();
tot = 0;
for (long long i = 1; i <= n; i++) head[i] = 0;
while (!S.empty()) S.pop();
for (long long i = 1; i <= 2 * n; i++) {
RST::head[i] = 0;
dfn[i] = 0;
low[i] = 0;
}
RST::tot = 0, id = 0;
RST::ANS = 0;
for (long long i = 1; i <= m; i++) {
long long x = read(), y = read();
made(x, y);
made(y, x);
}
sum = n;
N = 0;
tarjan(A, A);
S.pop();
RST::dfs(A, 0);
printf("%lld\n", RST::ANS);
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
bool vis1[N], vis2[N], q;
int t, n, m, a, b, cnt1, cnt2, cnt;
vector<int> adj[N];
void _clear() {
cnt1 = cnt2 = 0;
for (int i = 1; i <= n; i++) {
adj[i].clear();
vis1[i] = vis2[i] = 0;
}
}
void dfs1(int u) {
vis1[u] = 1;
cnt++;
if (u == b) q = 1;
for (auto x : adj[u])
if (!vis1[x] && x != a) dfs1(x);
}
void dfs2(int u) {
vis2[u] = 1;
cnt++;
if (u == a) q = 1;
for (auto x : adj[u])
if (!vis2[x] && x != b) dfs2(x);
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d%d%d", &n, &m, &a, &b);
_clear();
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
for (auto x : adj[a]) {
q = cnt = 0;
if (!vis1[x]) {
dfs1(x);
if (!q) cnt1 += cnt;
}
}
for (auto x : adj[b]) {
q = cnt = 0;
if (!vis2[x]) {
dfs2(x);
if (!q) cnt2 += cnt;
}
}
printf("%lld\n", 1LL * cnt1 * cnt2);
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int cases, cities, roads, nodeA, nodeB, cityA, cityB, coloredA,
coloredB, colored;
vector<vector<long long int>> graph;
vector<long long int> colors;
bitset<(int)2e5 + 1> vis;
void coloringdfs(long long int node) {
colors[node]++;
vis[node] = true;
for (auto child : graph[node])
if (!vis[child]) coloringdfs(child);
}
void countingdfs(long long int node) {
colored++;
vis[node] = true;
for (auto child : graph[node])
if (!vis[child] && colors[child] != 2) countingdfs(child);
}
void coloringTheNodes() {
nodeA--, nodeB--;
colors[nodeA] = colors[nodeB] = -1;
vis[nodeB] = true;
vis[nodeA] = false;
coloringdfs(nodeA);
vis.reset();
vis[nodeB] = false;
vis[nodeA] = true;
coloringdfs(nodeB);
}
void countingTheColoredNodes() {
vis.reset();
vis[nodeB] = true;
countingdfs(nodeA);
coloredA = colored - 1;
colored = 0;
countingdfs(nodeB);
coloredB = colored - 1;
}
int main() {
scanf("%lld", &cases);
while (cases--) {
colored = coloredA = coloredB = 0;
graph.clear();
colors.clear();
vis.reset();
scanf("%lld %lld %lld %lld", &cities, &roads, &nodeA, &nodeB);
graph.resize(cities);
colors.resize(cities);
while (roads--) {
scanf("%lld %lld", &cityA, &cityB);
cityA--, cityB--;
graph[cityA].push_back(cityB);
graph[cityB].push_back(cityA);
}
coloringTheNodes();
countingTheColoredNodes();
printf("%lld\n", coloredA * coloredB);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int dxk[] = {0, 0, 1, -1, 1, 1, -1, -1};
long long int dyk[] = {1, -1, 0, 0, 1, -1, 1, -1};
long long int ll_max(long long int a, long long int b, long long int c) {
return max(a, max(b, c));
}
int int_max(int a, int b, int c) { return max(a, max(b, c)); }
long long int ll_min(long long int a, long long int b, long long int c) {
return min(a, min(b, c));
}
int int_min(int a, int b, int c) { return min(a, min(b, c)); }
long long int max(int a, long long int b) { return max((long long int)a, b); }
long long int min(int a, long long int b) { return min((long long int)a, b); }
long long int min(long long int a, int b) { return min(a, (long long int)b); }
long long int max(long long int a, int b) { return max(a, (long long int)b); }
long long int dx[] = {0, 0, 1, -1};
long long int dy[] = {1, -1, 0, 0};
long long int power(long long int a, long long int b) {
if (a == 1) return 1;
if (b == 0) return 1;
long long int c = power(a, b / 2);
long long int res = 1;
if (b % 2) {
res = (c * c) % 1000000007;
res *= a;
res %= 1000000007;
} else
res = ((c * c) % 1000000007);
return res;
}
long long int modInv(long long int a) {
return power(a, 1000000007 - 2) % 1000000007;
}
long long int fact[1], inv[1];
void factorial(long long int n) {
fact[0] = 1;
for (long long int i = 1; i <= n; i++) {
fact[i] = fact[i - 1] * i;
fact[i] %= 1000000007;
}
}
void InvFactorial(long long int n) {
inv[0] = 1;
for (long long int i = 1; i <= n; i++) inv[i] = modInv(fact[i]);
}
long long int ncr(long long int n, long long int r) {
if (n < r || n < 0 || r < 0) return 0;
long long int b = inv[n - r];
long long int c = inv[r];
long long int a = fact[n] * b;
a %= 1000000007;
a *= c;
a %= 1000000007;
return a;
}
bool visited[200001];
vector<int> v[200001];
int par[200001];
int size;
int t;
void dfs(int i) {
size++;
visited[i] = 1;
for (long long int j = 0; j < v[i].size(); j++) {
if (par[v[i][j]] != -1 && par[v[i][j]] != t) t = -1;
if (visited[v[i][j]]) continue;
dfs(v[i][j]);
}
}
void solve(int countu) {
int n, m, a, b;
cin >> n >> m >> a >> b;
if (a > b) swap(a, b);
for (long long int i = 1; i < n + 1; i++) {
v[i].clear();
par[i] = -1;
}
for (long long int i = 1; i < n + 1; i++) visited[i] = 0;
queue<int> q, q1;
for (long long int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
if (x > y) swap(x, y);
if (x == a && y == b) continue;
if (x == a) {
if (visited[y]) continue;
if (par[y] == b) {
visited[y] = 1;
par[y] = -2;
continue;
}
par[y] = a;
q.push(y);
} else if (x == b) {
if (visited[y]) continue;
if (par[y] == a) {
visited[y] = 1;
par[y] = -2;
continue;
}
par[y] = b;
q1.push(y);
} else if (y == a) {
if (visited[x]) continue;
if (par[x] == b) {
visited[x] = 1;
par[x] = -2;
continue;
}
par[x] = a;
q.push(x);
} else if (y == b) {
if (visited[x]) continue;
if (par[x] == a) {
visited[x] = 1;
par[x] = -2;
continue;
}
par[x] = b;
q1.push(x);
} else {
v[x].push_back(y);
v[y].push_back(x);
}
}
visited[a] = 1;
visited[b] = 1;
long long int a1 = 0, b1 = 0;
while (!q.empty()) {
int i = q.front();
q.pop();
if (visited[i]) continue;
size = 0;
t = a;
if (par[i] == -2) t = -1;
dfs(i);
if (t == a) a1 += size;
}
while (!q1.empty()) {
int i = q1.front();
q1.pop();
if (visited[i]) continue;
size = 0;
t = b;
if (par[i] == -2) t = -1;
dfs(i);
if (t == b) b1 += size;
}
cout << a1 * b1 << '\n';
}
int main() {
int t = 1;
cin >> t;
int countu = 1;
while (t--) {
solve(countu);
countu++;
}
}
|
#include <bits/stdc++.h>
using namespace std;
struct node {
bool visited[2];
vector<int> neighs;
};
vector<node> graph;
void bfs(int mode, int v, int no) {
node currN = graph[v];
currN.visited[mode] = true;
queue<int> q;
for (int i = 0; i < currN.neighs.size(); i++) {
q.push(currN.neighs[i]);
}
while (q.size()) {
int nv = q.front();
q.pop();
if (!graph[nv].visited[mode] && nv != no) {
graph[nv].visited[mode] = true;
int sz = graph[nv].neighs.size();
for (int i = 0; i < sz; i++) q.push(graph[nv].neighs[i]);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
graph.clear();
int n, m, a, b;
cin >> n >> m >> a >> b;
graph.resize(n + 1);
for (int i = 0; i < m; i++) {
int v1, v2;
cin >> v1 >> v2;
graph[v1].neighs.push_back(v2);
graph[v2].neighs.push_back(v1);
}
bfs(0, a, b);
bfs(1, b, a);
long long int ca = 0;
long long int cb = 0;
for (int i = 1; i < n + 1; i++) {
node currN = graph[i];
if (currN.visited[0] && !currN.visited[1] && i != a) {
ca++;
} else if (!currN.visited[0] && currN.visited[1] && i != b) {
cb++;
}
}
cout << ca * cb << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long dis[200009];
bool mark[200009], vis[200009];
long long nodes, edges;
void dijkstra(int s, int av, vector<long long> ed[]) {
long long i, j, sz, u, v;
for (i = 1; i <= nodes; i++) {
dis[i] = 1e18;
vis[i] = false;
}
dis[s] = 0LL;
priority_queue<long long, vector<long long>, greater<long long> > pq;
pq.push(s);
while (!pq.empty()) {
u = pq.top();
pq.pop();
if (vis[u]) continue;
vis[u] = true;
sz = ed[u].size();
for (i = 0; i < sz; i++) {
v = ed[u][i];
if (v == av) continue;
if (dis[v] > dis[u] + ed[u][i]) {
dis[v] = dis[u] + ed[u][i];
pq.push(v);
}
}
}
return;
}
int main() {
long long t, i, j, k, l, node1, node2, ans, a, b;
cin >> t;
while (t--) {
cin >> nodes >> edges >> a >> b;
vector<long long> ed[nodes + 10];
for (i = 1; i <= nodes; i++) mark[i] = false;
for (i = 1; i <= edges; i++) {
cin >> node1 >> node2;
ed[node1].push_back(node2);
ed[node2].push_back(node1);
}
dijkstra(a, 0, ed);
if (dis[b] == 1e18) {
cout << 0 << endl;
continue;
}
dijkstra(a, b, ed);
long long xa = 0, xb = 0;
for (i = 1; i <= nodes; i++) {
if (i == a) continue;
if (dis[i] < 1e18) {
xa++;
mark[i] = true;
}
}
dijkstra(b, a, ed);
for (i = 1; i <= nodes; i++) {
if (i == b) continue;
if (dis[i] < 1e18) {
if (mark[i])
xa--;
else
xb++;
}
}
cout << xa * xb << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long fac[2000005];
void pre() {
fac[0] = 1;
fac[1] = 1;
for (int i = 2; i < 200005; i++) fac[i] = (i * fac[i - 1]) % 1000000007;
}
long long power(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = (res * a) % 1000000007;
a = (a * a) % 1000000007;
b = b / 2;
}
return res % 1000000007;
}
long long ncr(long long n, long long r) {
return ((fac[n] * power(fac[r], 1000000007 - 2)) % 1000000007 *
power(fac[n - r], 1000000007 - 2)) %
1000000007;
}
long long visa[200005];
long long visb[200005];
vector<long long> adj[2000005];
long long a, b;
void dfs1(long long s) {
visa[s] = 1;
for (auto it : adj[s])
if (!visa[it] && it != b) dfs1(it);
}
void dfs2(long long s) {
visb[s] = 1;
for (auto it : adj[s])
if (!visb[it] && it != a) dfs2(it);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
map<long long, long long> mp1;
map<long long, long long> mp2;
long long n, m;
cin >> n >> m >> a >> b;
for (int i = 0; i < m; i++) {
long long a1, b1;
cin >> a1 >> b1;
adj[a1].push_back(b1);
adj[b1].push_back(a1);
}
dfs1(a);
dfs2(b);
long long c1 = 0, c2 = 0;
for (int i = 1; i <= n; i++)
if (visa[i] && !visb[i] && i != a && i != b) c1++;
for (int i = 1; i <= n; i++)
if (visb[i] && i != b && i != a && !visa[i]) c2++;
long long ans = c1 * c2;
cout << ans << endl;
for (int i = 1; i <= n; i++) {
adj[i].clear();
visa[i] = 0;
visb[i] = 0;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 10, M = 1e6 + 10;
bool vis[N], visa[N];
long long n, i, m, las[N], nex[M], t, tot, tov[M], x, y, a, b, pre[N], suma,
sumb, ans;
void ins(long long x, long long y) {
tov[++tot] = y, nex[tot] = las[x], las[x] = tot;
}
void dfs(long long x, long long c) {
long long i, y;
vis[x] = true;
for (i = las[x]; i; i = nex[i]) {
y = tov[i];
if (vis[y] || y == c) continue;
dfs(y, c);
}
}
int main() {
for (scanf("%d", &t); t; t--) {
scanf("%lld%lld%lld%lld", &n, &m, &a, &b), tot = suma = sumb = 0;
memset(las, 0, sizeof(las));
for (i = 1; i <= m; i++) {
scanf("%lld%lld", &x, &y), ins(x, y), ins(y, x);
}
memset(vis, false, sizeof(vis));
dfs(a, b);
memcpy(visa, vis, sizeof(visa));
memset(vis, false, sizeof(vis));
dfs(b, a);
for (i = 1; i <= n; i++) {
if (visa[i] && !vis[i] && i != a) suma++;
if (!visa[i] && vis[i] && i != b) sumb++;
}
ans = suma * sumb;
printf("%lld\n", ans);
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> v[N];
int n, m, A, B, t;
int vis[N];
int f[N][2], Aa[2];
int c;
void dfs(int l, int r, int fa) {
vis[l] = 1;
if (l == r) {
return;
}
for (auto k : v[l]) {
if (vis[k]) {
continue;
}
dfs(k, r, fa);
}
}
int main() {
ios::sync_with_stdio(false);
for (cin >> t; t; t--) {
cin >> n >> m >> A >> B;
c = 0;
Aa[0] = Aa[1] = 0;
for (int i = 1; i <= n; i++)
v[i].clear(), vis[i] = 0, f[i][0] = f[i][1] = 0;
for (int i = 1; i <= m; i++) {
int s, t;
cin >> s >> t;
v[s].push_back(t);
v[t].push_back(s);
}
dfs(A, B, A);
for (int i = 1; i <= n; i++) {
if (!vis[i]) Aa[c]++;
vis[i] = 0;
}
++c;
dfs(B, A, B);
for (int i = 1; i <= n; i++) {
if (!vis[i]) Aa[c]++;
}
cout << 1ll * Aa[0] * Aa[1] << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long maxn = 2e5 + 10;
vector<long long> adj[maxn];
long long vis[maxn];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(9);
long long t;
cin >> t;
while (t--) {
long long n, m, a, b;
cin >> n >> m >> a >> b;
for (long long i = 0; i <= n; ++i) {
vis[i] = 0;
adj[i].clear();
}
for (long long i = 1; i <= m; ++i) {
long long u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vis[b] = 1;
queue<long long> q;
q.push(a);
vis[a] = 1;
while (!q.empty()) {
long long v = q.front();
q.pop();
for (long long u : adj[v]) {
if (vis[u] == 0) {
vis[u] = 1;
q.push(u);
}
}
}
long long cnt1 = 0;
for (long long i = 0; i <= n; ++i) {
if (vis[i] == 1) {
++cnt1;
}
}
cnt1 = n - cnt1;
for (long long i = 0; i <= n; ++i) {
vis[i] = 0;
}
vis[a] = 1;
q.push(b);
vis[b] = 1;
while (!q.empty()) {
long long v = q.front();
q.pop();
for (long long u : adj[v]) {
if (vis[u] == 0) {
vis[u] = 1;
q.push(u);
}
}
}
long long cnt2 = 0;
for (long long i = 0; i <= n; ++i) {
if (vis[i] == 1) ++cnt2;
}
cnt2 = n - cnt2;
cout << cnt1 * cnt2 << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int ret = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar();
return w * ret;
}
int n, m, a, b;
int tot = 1;
int edge[1000010];
int nxt[1000010];
int hd[200010];
inline void add_edge(int u, int v) {
edge[tot] = v;
nxt[tot] = hd[u];
hd[u] = tot++;
}
int book[200010];
int dfs(int p, int root) {
if ((root == a && p == b) || (root == b && p == a)) return -9999999;
book[p] = 1;
int subtree = 0;
int sz = 1;
int cnt = 0;
for (int i = hd[p]; i; i = nxt[i])
if (!book[edge[i]]) {
subtree++;
int x = dfs(edge[i], root);
if (p == root && x > 0) cnt += x;
if (p != root) sz += x;
}
if (p == root && subtree <= 1)
return 0;
else if (p == root)
return cnt;
else
return sz;
}
int main() {
int t = read();
while (t--) {
tot = 1;
n = read(), m = read(), a = read(), b = read();
for (int i = 0; i <= 2 * m; i++) edge[i] = 0;
for (int i = 0; i <= 2 * m; i++) nxt[i] = 0;
for (int i = 0; i <= n; i++) hd[i] = 0;
for (int i = 0; i <= n; i++) book[i] = 0;
for (int i = 1; i <= m; i++) {
int u = read(), v = read();
add_edge(u, v);
add_edge(v, u);
}
long long s1 = (long long)dfs(a, a);
for (int i = 0; i <= n; i++) book[i] = 0;
long long s2 = (long long)dfs(b, b);
printf("%lld\n", s1 * s2);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
using v2d = vector<vector<T> >;
template <class T>
bool uin(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
return a < b ? (a = b, true) : false;
}
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int maxN = 2e5 + 10;
const int maxM = 5e5 + 10;
int n, m, a, b;
vector<int> adj[maxN];
int fa[maxN], fb[maxN];
long long ca = 0, cb = 0;
void DFSa(int u) {
if (u == b) {
return;
}
fa[u] = 1;
for (auto &v : adj[u]) {
if (!fa[v]) {
DFSa(v);
}
}
}
void DFSb(int u) {
if (u == a) {
return;
}
fb[u] = 1;
for (auto &v : adj[u]) {
if (!fb[v]) {
DFSb(v);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
cin >> n >> m >> a >> b;
for (int i = 1; i <= (int)(n); ++i) {
adj[i].clear();
fa[i] = fb[i] = 0;
}
while (m--) {
int u, v;
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
DFSa(a);
DFSb(b);
ca = cb = 0;
for (int i = 1; i <= (int)(n); ++i) {
if (fa[i]) {
ca += !fb[i];
}
if (fb[i]) {
cb += !fa[i];
}
}
cout << (ca - 1) * (cb - 1) << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 1;
vector<long long> v[N];
vector<long long> vis(N);
long long cnt = 0;
void dfs(long long x, long long p) {
vis[x] = 1;
cnt++;
for (auto i : v[x]) {
if (i == p) continue;
if (vis[i] == 1) continue;
dfs(i, x);
}
}
vector<long long> bfs(long long a, long long b, long long n) {
vector<long long> vis(n + 1, 0);
set<long long> s;
for (auto x : v[b]) s.insert(x);
queue<long long> q;
q.push(a);
vis[a] = 1;
while (!q.empty()) {
long long x = q.front();
q.pop();
for (auto i : v[x]) {
if (vis[i]) continue;
if (i == b) {
s.erase(x);
} else {
q.push(i);
vis[i] = 1;
}
}
}
vector<long long> res;
for (auto x : s) res.push_back(x);
return res;
}
void solve() {
long long n, m, a, b;
cin >> n >> m >> a >> b;
for (long long i = 0; i < m; i++) {
long long x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
vector<long long> vb = bfs(a, b, n);
vector<long long> va = bfs(b, a, n);
v[b] = vb;
v[a] = va;
long long ans = 0;
for (long long i = 1; i < n + 1; i++) vis[i] = 0;
cnt = 0;
dfs(a, a);
ans = (cnt - 1);
cnt = 0;
dfs(b, b);
ans *= (cnt - 1);
cout << ans << endl;
for (long long i = 0; i < n + 1; i++) v[i].clear();
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc;
cin >> tc;
while (tc--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3F3F3F3F;
const int NULO = -1;
const double EPS = 1e-10;
int cmp() __attribute__((always_inline));
int cmp(double x, double y = 0, double tol = EPS) {
return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}
void dfs(int i, vector<vector<int> >& graph, vector<bool>& vis, int proibido) {
vis[i] = true;
for (auto p : graph[i]) {
if (!vis[p] && p != proibido) dfs(p, graph, vis, proibido);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int n, m, a, b;
cin >> n >> m >> a >> b;
vector<vector<int> > graph(n);
vector<bool> vis(n, false);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
graph[x - 1].push_back(y - 1);
graph[y - 1].push_back(x - 1);
}
dfs(a - 1, graph, vis, b - 1);
long long sumA = 0, sumB = 0;
for (int i = 0; i < n; i++)
if (!vis[i]) sumA++;
sumA--;
vis.assign(n, false);
dfs(b - 1, graph, vis, a - 1);
for (int i = 0; i < n; i++)
if (!vis[i]) sumB++;
sumB--;
cout << sumA * sumB << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> adj[200005];
set<long long> bac[200005];
long long mas[200005];
long long a;
void ba(long long u) {
if (u == a) return;
if (mas[u] == 0 || mas[u] == 2) return;
mas[u] = 2;
while (!bac[u].empty()) {
long long a1 = *bac[u].begin();
bac[u].erase(a1);
ba(a1);
}
}
int main() {
long long t;
cin >> t;
for (long long t1 = 0; t1 < t; t1++) {
long long n, m, b, c, d;
cin >> n >> m >> a >> b;
for (long long i = 0; i < m; i++) {
cin >> c >> d;
adj[c].push_back(d);
adj[d].push_back(c);
}
queue<long long> q;
mas[a] = 1;
q.push(a);
while (!q.empty()) {
c = q.front();
q.pop();
if (c != b) {
for (long long i = 0; i < adj[c].size(); i++) {
if (mas[adj[c][i]] == 0) {
q.push(adj[c][i]);
}
mas[adj[c][i]] = 1;
bac[adj[c][i]].insert(c);
}
}
}
ba(b);
mas[a] = 2;
c = 0;
d = 0;
for (long long i = 1; i <= n; i++) {
adj[i].clear();
bac[i].clear();
if (mas[i] == 0) c++;
if (mas[i] == 1) d++;
mas[i] = 0;
}
cout << c * d << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b;
vector<int> adj[200007];
bool v[200007];
bool touch[200007];
bool flag;
void DFS1(int x, int des) {
v[x] = 1;
for (auto y : adj[x]) {
if (y == des) flag = 1;
if (v[y]) continue;
DFS1(y, des);
}
return;
}
void DFS2(int x, long long int &cnt) {
v[x] = 1;
for (auto y : adj[x]) {
if (v[y]) continue;
cnt++;
DFS2(y, cnt);
}
return;
}
int main() {
int i, j;
int t;
scanf("%d", &t);
while (t--) {
scanf("%d %d %d %d", &n, &m, &a, &b);
for (i = 1; i <= n; i++) adj[i].clear();
for (i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
long long int cnt1 = 0;
long long int cnt2 = 0;
flag = 0;
memset(v, 0, n + 1);
v[b] = 1;
DFS1(a, b);
v[b] = 0;
DFS2(b, cnt1);
memset(v, 0, n + 1);
v[a] = 1;
DFS1(b, a);
v[a] = 0;
DFS2(a, cnt2);
if (1 != 0 && 0 == 1) printf(">>> %lld %lld\n", cnt1, cnt2);
if (flag)
printf("%lld\n", cnt1 * cnt2);
else
printf("0\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE = 1e5 + 10, inf = 2 * 1e9;
const long long INF = 1e18, N = 59778, MOD = 998244353;
const double eps = 1e-6, PI = 20 / 7;
void files() {
freopen("input.txt", " r", stdin);
freopen("output.txt", "w", stdout);
}
void solved() {
int n, m, a, b;
cin >> n >> m >> a >> b;
a--;
b--;
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
struct node {
int sz;
int pr;
node(int sz_, int pr_) {
sz = sz_;
pr = pr_;
}
node() {}
};
struct DSU {
vector<node> dsu;
DSU(int n) { dsu.resize(n); }
int find_pr(int v) {
if (dsu[v].pr == v)
return v;
else
return find_pr(dsu[v].pr);
}
void un(int a, int b) {
a = find_pr(a);
b = find_pr(b);
if (a == b) return;
if (dsu[a].sz < dsu[b].sz) swap(a, b);
dsu[b].pr = a;
dsu[a].sz += dsu[b].sz;
return;
}
};
DSU d = DSU(n);
for (int i = 0; i < n; i++) {
d.dsu[i].pr = i;
d.dsu[i].sz = 1;
}
for (int i = 0; i < n; i++) {
if (i == a || i == b) continue;
for (auto to : g[i]) {
if (to == a || to == b) continue;
d.un(i, to);
}
}
long long ans = 0;
long long cnt1 = 0;
vector<int> used(n, 0);
for (auto to : g[a]) {
if (to == a || to == b) continue;
int p = d.find_pr(to);
if (!used[p]) {
cnt1 += d.dsu[p].sz;
used[p] = 1;
}
}
long long cnt2 = 0;
long long cnt3 = 0;
for (auto to : g[b]) {
if (to == a || to == b) continue;
int p = d.find_pr(to);
if (used[p] != 2) {
cnt2 += d.dsu[p].sz;
if (used[p] == 1) {
cnt3 += d.dsu[p].sz;
}
used[p] = 2;
}
}
cnt2 -= cnt3;
cnt1 -= cnt3;
cout << cnt1 * cnt2 << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
solved();
}
}
|
#include <bits/stdc++.h>
using namespace std;
char buf[1 << 20], *_ = buf, *__ = buf;
template <class T>
inline bool read(T &x) {
x = 0;
char c = (_ == __ && (__ = (_ = buf) + fread(buf, 1, 1 << 20, stdin), _ == __)
? EOF
: *_++);
bool f = 0;
while (c < 48 || c > 57) {
if (c == EOF) return 0;
f ^= (c == '-'),
c = (_ == __ &&
(__ = (_ = buf) + fread(buf, 1, 1 << 20, stdin), _ == __)
? EOF
: *_++);
}
while (47 < c && c < 58)
x = (x << 3) + (x << 1) + (c ^ 48),
c = (_ == __ && (__ = (_ = buf) + fread(buf, 1, 1 << 20, stdin), _ == __)
? EOF
: *_++);
if (f) x = -x;
return 1;
}
template <class T>
inline bool read(T &a, T &b) {
return read(a) && read(b);
}
template <class T>
inline bool read(T &a, T &b, T &c) {
return read(a) && read(b) && read(c);
}
const long long MAXN = 2e5 + 8, mod = 1e9 + 7, inf = 0x3f3f3f3f;
struct E {
int y, nt;
} e[MAXN * 20];
int head[MAXN], cnt;
inline void add(int x, int y) {
e[++cnt].y = y;
e[cnt].nt = head[x];
head[x] = cnt;
}
int low[MAXN], dfn[MAXN], tarcnt, root;
bool cut[MAXN];
int vb;
long long na, nb;
long long n, m, a, b;
int tarjan(int x) {
int siz = 1;
if (x == b) vb = 1;
low[x] = dfn[x] = ++tarcnt;
int flag = 0;
for (int i = head[x]; i; i = e[i].nt) {
int y = e[i].y;
if (!dfn[y]) {
int tmp = tarjan(y);
siz += tmp;
if (x == root && vb) {
vb = 0;
siz -= tmp;
}
low[x] = min(low[x], low[y]);
if (low[y] >= dfn[x]) {
flag++;
if (x ^ root || flag > 1) {
cut[x] = 1;
if (x == b) nb += tmp;
}
}
} else
low[x] = min(low[x], dfn[y]);
}
return siz;
}
int main() {
srand(666);
int t;
read(t);
while (t--) {
read(n, m), read(a, b);
tarcnt = cnt = 0;
na = nb = 0;
for (int i = 0; i <= n; ++i) {
head[i] = 0;
low[i] = 0;
dfn[i] = 0;
cut[i] = 0;
}
for (int i = 0, x, y; i < m; ++i) {
read(x, y);
add(x, y);
add(y, x);
}
na = tarjan(root = a);
if (!cut[a] || !cut[b]) {
puts("0");
continue;
}
long long ans = nb * (na - 1);
printf("%I64d\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, t, cnt[N], a, b, vis[N], visId = 1;
vector<int> adj[N];
void DFS1(int node) {
vis[node] = visId;
cnt[node]++;
for (int i = 0; i < adj[node].size(); i++) {
if (vis[adj[node][i]] == visId || adj[node][i] == a) continue;
DFS1(adj[node][i]);
}
}
void DFS2(int node) {
vis[node] = visId;
cnt[node]--;
for (int i = 0; i < adj[node].size(); i++) {
if (vis[adj[node][i]] == visId || adj[node][i] == b) continue;
DFS2(adj[node][i]);
}
}
void clear_all() {
for (int i = 1; i <= n; i++) adj[i].clear(), cnt[i] = 0;
++visId;
}
int main() {
cin >> t;
while (t--) {
scanf("%d%d%d%d", &n, &m, &a, &b);
int u, v;
while (m--) {
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
DFS1(b);
++visId;
DFS2(a);
int cnt1 = 0, cnt_1 = 0;
for (int i = 1; i <= n; i++) {
cnt1 += (cnt[i] == 1);
cnt_1 += (cnt[i] == -1);
}
printf("%lld\n", 1ll * (cnt1 - 1) * (cnt_1 - 1));
clear_all();
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long fac[2000005];
void pre() {
fac[0] = 1;
fac[1] = 1;
for (int i = 2; i < 200005; i++) fac[i] = (i * fac[i - 1]) % 1000000007;
}
long long power(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = (res * a) % 1000000007;
a = (a * a) % 1000000007;
b = b / 2;
}
return res % 1000000007;
}
long long ncr(long long n, long long r) {
return ((fac[n] * power(fac[r], 1000000007 - 2)) % 1000000007 *
power(fac[n - r], 1000000007 - 2)) %
1000000007;
}
long long visa[200005];
long long visb[200005];
vector<long long> adj[2000005];
long long a, b;
void dfs1(long long s) {
visa[s] = 1;
for (auto it : adj[s])
if (!visa[it] && it != b) dfs1(it);
}
void dfs2(long long s) {
visb[s] = 1;
for (auto it : adj[s])
if (!visb[it] && it != a) dfs2(it);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
map<long long, long long> mp1;
map<long long, long long> mp2;
long long n, m;
cin >> n >> m >> a >> b;
for (int i = 0; i < m; i++) {
long long a1, b1;
cin >> a1 >> b1;
adj[a1].push_back(b1);
adj[b1].push_back(a1);
}
dfs1(a);
dfs2(b);
long long c1 = 0, c2 = 0;
for (int i = 1; i <= n; i++)
if (visa[i] && !visb[i] && i != a && i != b) c1++;
for (int i = 1; i <= n; i++)
if (visb[i] && i != b && i != a && !visa[i]) c2++;
long long ans = c1 * c2;
long long count = 0;
for (int i = 1; i <= n; i++) {
if (i != a && i != b && !visa[i] && !visb[i]) count++;
}
ans += (count * (count - 1)) / 2;
cout << ans << endl;
for (int i = 1; i <= n; i++) {
adj[i].clear();
visa[i] = 0;
visb[i] = 0;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
return (a.first > b.first);
}
long long int cnt = 0;
void dfs(long long int x, vector<long long int> arr[], bool visit[],
long long int val[], long long int xyz, bool freq1[], bool freq2[]) {
visit[x] = true;
cnt++;
val[x] = xyz;
if (freq1[x] == 1) {
freq1[val[x]] = 1;
}
if (freq2[x] == 1) {
freq2[val[x]] = 1;
}
for (auto i : arr[x]) {
if (visit[i] == 0) dfs(i, arr, visit, val, xyz, freq1, freq2);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long int t;
cin >> t;
while (t--) {
long long int n, m, a, b;
cin >> n >> m >> a >> b;
cnt = 0;
vector<long long int> arr[n + 2], brr[n + 3], crr[3];
vector<pair<long long int, long long int>> err;
bool freq1[700004] = {0}, freq2[700004] = {0};
for (long long int i = 0; i < m; i++) {
long long int x, y;
cin >> x >> y;
arr[x].push_back(y);
arr[y].push_back(x);
if (x != a && x != b && y != a && y != b) {
brr[x].push_back(y);
brr[y].push_back(x);
}
if (x == a && y != b) {
freq1[y] = true;
}
if (y == a && x != b) {
freq1[x] = true;
}
if (x == b && y != a) {
freq2[y] = true;
}
if (y == b && x != a) {
freq2[x] = true;
}
}
bool visit[200004] = {0};
long long int val[200004];
long long int xyz = 500000;
long long int size[700003] = {0};
vector<long long int> frr, grr, hrr;
long long int sum = 0;
for (long long int i = 1; i <= n; i++) {
if (visit[i] == 0 && i != a && i != b) {
cnt = 0;
val[i] = xyz;
dfs(i, brr, visit, val, xyz, freq1, freq2);
xyz++;
size[val[i]] = cnt;
err.push_back(make_pair(cnt, val[i]));
sum += cnt;
}
}
long long int ct = 0;
long long int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
for (long long int i = 0; i < err.size(); i++) {
ct += err[i].first;
long long int k = sum - ct;
sum4 += err[i].first * k;
if (freq1[err[i].second] == 1) {
sum1 += err[i].first;
frr.push_back(err[i].first);
}
if (freq2[err[i].second] == 1) {
sum2 += err[i].first;
grr.push_back(err[i].first);
}
if (freq1[err[i].second] == 1 && freq2[err[i].second] == 1) {
sum3 += err[i].first;
hrr.push_back(err[i].first);
}
}
ct = 0;
long long int sum5 = 0, sum6 = 0, sum7 = 0;
for (long long int i = 0; i < grr.size(); i++) {
ct += grr[i];
long long int k = sum2 - ct;
sum5 += grr[i] * k;
}
ct = 0;
for (long long int i = 0; i < hrr.size(); i++) {
ct += hrr[i];
long long int k = sum3 - ct;
sum6 += hrr[i] * k;
}
ct = 0;
for (long long int i = 0; i < frr.size(); i++) {
ct += frr[i];
long long int k = sum1 - ct;
sum7 += frr[i] * k;
}
cout << sum4 - sum5 - sum7 + sum6 << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> g;
long long n, m, a, b;
vector<bool> used;
long long cnt = 0;
pair<bool, bool> dfs(long long v) {
used[v] = true;
++cnt;
bool first = false;
bool second = false;
for (long long u : g[v]) {
if (u == a)
first = true;
else if (u == b)
second = true;
else if (!used[u]) {
auto p = dfs(u);
first = first || p.first;
second = second || p.second;
}
}
return {first, second};
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
cin >> n >> m >> a >> b;
--a;
--b;
g.resize(n);
g.assign(n, vector<long long>());
for (long long i = 0; i < m; ++i) {
long long v, u;
cin >> v >> u;
--v;
--u;
g[v].push_back(u);
g[u].push_back(v);
}
used.resize(n);
used.assign(n, false);
long long cnt1 = 0;
long long cnt2 = 0;
for (long long i = 0; i < n; ++i) {
if (i == a) continue;
if (i == b) continue;
if (!used[i]) {
cnt = 0;
auto p = dfs(i);
if (p.first && p.second) continue;
if (p.first) cnt1 += cnt;
if (p.second) cnt2 += cnt;
}
}
cout << cnt1 * cnt2 << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int p[200000][3];
int find(int x, int type) {
if (p[x][type] < 0) {
return x;
}
p[x][type] = find(p[x][type], type);
return p[x][type];
}
void merge(int x, int y, int type) {
x = find(x, type);
y = find(y, type);
if (x == y) return;
p[x][type] += p[y][type];
p[y][type] = x;
}
int unionsize(int x) { return -p[x][0]; }
int main() {
int c;
scanf("%d\n", &c);
for (int t = 0; t < c; t++) {
int n, m, a, b;
scanf("%d %d %d %d\n", &n, &m, &a, &b);
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
p[i][j] = -1;
}
}
a--;
b--;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d\n", &u, &v);
u--;
v--;
if (u != a && u != b && v != a && v != b) {
merge(u, v, 0);
}
if (u != a && v != a) {
merge(u, v, 1);
}
if (u != b && v != b) {
merge(u, v, 2);
}
}
long long aside = 0;
long long bside = 0;
for (int i = 0; i < n; i++) {
if (i == a || i == b) continue;
if (find(b, 1) == find(i, 1) && find(a, 2) != find(i, 2)) {
bside += 1;
}
if (find(b, 1) != find(i, 1) && find(a, 2) == find(i, 2)) {
aside += 1;
}
}
printf("%lld\n", aside * bside);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
hash<string> hfn;
const int inf = 2e9;
const long long mod = 1e9 + 7;
const long double eps = 1e-7;
const long long biginf = 1e18;
int n, m, a, b;
vector<vector<int> > g;
vector<int> used;
long long cnt1 = 0, cnt2 = 0;
void dfs1(int cur) {
used[cur] = true;
cnt1++;
for (auto to : g[cur]) {
if (to != b && !used[to]) dfs1(to);
}
}
void dfs2(int cur) {
used[cur] = true;
cnt2++;
for (auto to : g[cur]) {
if (to != a && !used[to]) dfs2(to);
}
}
void solve() {
cin >> n >> m >> a >> b;
a--, b--;
g.resize(n);
used.resize(n, false);
for (int i = 0; i < m; i++) {
int f, t;
cin >> f >> t;
f--, t--;
g[f].push_back(t);
g[t].push_back(f);
}
dfs1(a);
fill((used).begin(), (used).end(), false);
dfs2(b);
cout << (n - cnt1 - 1) * (n - cnt2 - 1) << endl;
g.clear();
used.clear();
cnt1 = 0;
cnt2 = 0;
}
void multisolve() {
int t;
cin >> t;
while (t--) solve();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
multisolve();
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> g[200005];
int t;
int n, m, a, b;
bool visited[200005];
int val[200005];
bool flag = false;
void dfs1(int i, int v) {
if (i == b) return;
visited[i] = true;
val[i] += v;
for (auto it : g[i]) {
if (!visited[it]) dfs1(it, v);
}
}
void dfs2(int i, int v) {
if (i == a) return;
visited[i] = true;
val[i] += v;
for (auto it : g[i]) {
if (!visited[it]) dfs2(it, v);
}
}
int main() {
cin >> t;
while (t--) {
cin >> n >> m >> a >> b;
for (int i = 0; i <= n; i++) {
visited[i] = false;
val[i] = 0;
g[i].clear();
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs1(a, 1);
for (long long int i = 1; i <= n; i++) visited[i] = false;
dfs2(b, -1);
long long int aa = 0, bb = 0;
for (long long int i = 1; i <= n; i++) {
if (val[i] == 1) aa++;
if (val[i] == -1) bb++;
}
cout << ((aa - 1) * (bb - 1)) << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
struct node {
int pre, to;
} a[500010 << 1];
int head[500010 << 2];
bool vis[500010];
bool flag;
int t, n, m, aa, bb, tot, cnt;
long long suma, sumb;
void addedge(int from, int to) {
a[++tot].to = to;
a[tot].pre = head[from];
head[from] = tot;
}
void dfs(int u, int fa, int tmp) {
vis[u] = 1;
for (int i = head[u]; i > 0; i = a[i].pre) {
int v = a[i].to;
if (v == fa || vis[v]) continue;
if (v != tmp) dfs(v, u, tmp);
}
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d%d%d", &n, &m, &aa, &bb);
suma = sumb = tot = 0;
for (int i = 1; i <= m * 2; ++i) {
head[i] = -1;
}
for (int i = 1; i <= m; ++i) {
int x, y;
scanf("%d%d", &x, &y);
addedge(x, y);
addedge(y, x);
}
dfs(aa, -1, bb);
for (int i = 1; i <= n; ++i) {
if (!vis[i] && i != bb) sumb++;
vis[i] = 0;
}
dfs(bb, -1, aa);
for (int i = 1; i <= n; ++i) {
if (!vis[i] && i != aa) suma++;
vis[i] = 0;
}
printf("%lld\n", suma * sumb);
}
return 0;
}
|
#include <bits/stdc++.h>
const int inf = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;
const int bound = 200010;
int n, z, cnt;
vector<int> graph[bound];
bool bscht[bound];
void init(int ziel) {
for (int i = 0; i < n; ++i) bscht[i] = false;
cnt = 1;
z = ziel;
bscht[z] = true;
}
void del() {
for (int i = 0; i < n; ++i) {
auto &v = graph[i];
v.erase(v.begin(), v.end());
}
}
void dfs(int jetzt) {
bscht[jetzt] = true;
++cnt;
for (auto next : graph[jetzt]) {
if (!bscht[next]) dfs(next);
}
}
int main() {
int t;
cin >> t;
while (t--) {
int m, a, b;
cin >> n >> m >> a >> b;
--a;
--b;
del();
while (m--) {
int x, y;
cin >> x >> y;
--x;
--y;
graph[x].push_back(y);
graph[y].push_back(x);
}
init(b);
dfs(a);
ll x = n - cnt;
init(a);
dfs(b);
ll y = n - cnt;
cout << x * y << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<long long> > &adj, vector<long long> &visited,
long long src, long long b) {
visited[src] = 1;
if (src == b) return;
for (long long i = 0; i < adj[src].size(); i++) {
if (!visited[adj[src][i]]) dfs(adj, visited, adj[src][i], b);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
long long v, e, a, b;
cin >> v >> e >> a >> b;
vector<vector<long long> > adj(v + 1);
for (long long i = 0; i < e; i++) {
long long v1, v2;
cin >> v1 >> v2;
adj[v1].push_back(v2);
adj[v2].push_back(v1);
}
vector<long long> visited(v + 1, 0);
vector<long long> visited2(v + 1, 0);
dfs(adj, visited, a, b);
dfs(adj, visited2, b, a);
long long n1 = 0, n2 = 0;
for (long long i = 0; i < v + 1; i++) {
if (!visited[i] && visited2[i])
n1++;
else if (visited[i] && !visited2[i])
n2++;
}
cout << n1 * n2 << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long po(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
void dfs(vector<vector<long long> > &adj, long long s, long long e,
vector<long long> &v, vector<bool> &vis) {
if (s == e) return;
vis[s] = true;
for (auto vx : adj[s]) {
if (!vis[vx]) {
dfs(adj, vx, e, v, vis);
}
}
v.push_back(s);
}
void solve() {
long long n, m, a, b;
cin >> n >> m >> a >> b;
vector<vector<long long> > adj(n + 1);
while (m--) {
long long x, y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<long long> v1;
vector<long long> v2;
vector<bool> vis(n + 1, false);
dfs(adj, a, b, v1, vis);
for (int i = 0; i < n + 1; i++) vis[i] = false;
dfs(adj, b, a, v2, vis);
sort((v1).begin(), (v1).end());
sort((v2).begin(), (v2).end());
long long n1 = 0, n2 = 0;
for (long long i = 0; i < v1.size(); i++) {
if (!binary_search(v2.begin(), v2.end(), v1[i]) and v1[i] != a) {
n1++;
}
}
for (long long i = 0; i < v2.size(); i++) {
if (!binary_search(v1.begin(), v1.end(), v2[i]) and v2[i] != b) n2++;
}
cout << n1 * n2 << '\n';
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int t, n, m, a, b;
set<int> ss1, ss2;
const int N = 2e5 + 1;
vector<int> e[N];
int c[N];
void dfs(int p, int cc) {
c[p] = cc;
for (auto v : e[p]) {
if (c[v] != 0) continue;
if (v == a || v == b) continue;
dfs(v, cc);
}
return;
}
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
while (cin >> t) {
while (t--) {
cin >> n >> m >> a >> b;
for (int i = (1); i < (n + 1); i++) {
e[i].clear();
c[i] = 0;
}
for (int i = (1); i < (m + 1); i++) {
int x, y;
cin >> x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
for (int i = (1); i < (n + 1); i++)
if (c[i] == 0 && i != a && i != b) dfs(i, i);
ss1.clear();
ss2.clear();
for (auto v : e[a]) ss1.insert(c[v]);
for (auto v : e[b]) ss2.insert(c[v]);
long long c1, c2;
c1 = c2 = 0;
for (int i = (1); i < (n + 1); i++) {
if (ss1.find(c[i]) != ss1.end() && ss2.find(c[i]) == ss2.end()) c1++;
if (ss1.find(c[i]) == ss1.end() && ss2.find(c[i]) != ss2.end()) c2++;
}
cout << c1 * c2 << '\n';
}
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5 + 5;
const int MAXM = 1e6 + 5;
const int MOD = 1e9 + 7;
const int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
const double PI = acos(-1.0);
const double EXP = 1e-8;
struct Edge {
int to, next;
} edge[MAXM];
int head[MAXN], tot;
void add_edge(int u, int v) {
edge[++tot].to = v;
edge[tot].next = head[u];
head[u] = tot;
}
bool vis[MAXN];
int belong[MAXN], scc;
int block[MAXN], du[2][MAXN];
int a, b, n, m;
void init() {
for (int i = 1; i <= n; i++) vis[i] = false, head[i] = 0;
tot = scc = 0;
}
void dfs(int u) {
vis[u] = true;
belong[u] = scc;
block[scc]++;
for (int i = head[u]; i; i = edge[i].next) {
int v = edge[i].to;
if (v == a)
if (!du[0][belong[u]]) du[0][belong[u]] = true;
if (v == b)
if (!du[1][belong[u]]) du[1][belong[u]] = true;
if (!vis[v]) dfs(v);
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
cin >> n >> m >> a >> b;
init();
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
add_edge(u, v);
add_edge(v, u);
}
vis[a] = vis[b] = true;
for (int i = 1; i <= n; i++) {
if (i == a || i == b) continue;
if (!vis[i]) {
belong[++scc] = 0;
block[scc] = 0;
du[0][scc] = 0;
du[1][scc] = 0;
dfs(i);
}
}
long long left = 0, right = 0;
if (scc == 1)
cout << 0 << endl;
else {
for (int i = 1; i <= scc; i++) {
if (du[0][i] && du[1][i]) continue;
if (du[0][i]) left += block[i];
if (du[1][i]) right += block[i];
}
cout << left * right << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v[200005];
int vis[200005];
int ans[200005];
void dfs(int node, int f) {
vis[node] = 1;
for (int i = 0; i < v[node].size(); i++) {
if (vis[v[node][i]]) continue;
ans[v[node][i]] += f;
dfs(v[node][i], f);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, m, a, b;
cin >> n >> m >> a >> b;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
vis[a] = 1;
vis[b] = 1;
dfs(a, 1);
for (int i = 0; i <= n; i++) {
vis[i] = 0;
}
vis[a] = 1;
vis[b] = 1;
dfs(b, -1);
long long pos = 0, neg = 0;
for (int i = 0; i <= n; i++) {
if (ans[i] > 0)
pos++;
else if (ans[i] < 0) {
neg++;
}
}
cout << pos * neg << '\n';
for (int i = 0; i <= n; i++) {
v[i].clear();
vis[i] = 0;
ans[i] = 0;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, m, a, b;
cin >> n >> m >> a >> b;
a--;
b--;
vector<vector<int> > graph(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
int x = 0;
vector<bool> vis(n);
function<void(int)> dfs = [&](int u) {
vis[u] = true;
for (int v : graph[u]) {
if (vis[v] || v == x) continue;
dfs(v);
}
};
x = b;
dfs(a);
int cnta = count(vis.begin(), vis.end(), false) - 1;
fill(vis.begin(), vis.end(), false);
x = a;
dfs(b);
int cntb = count(vis.begin(), vis.end(), false) - 1;
cout << (long long)cnta * cntb << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A>
string to_string(A);
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool f = 1;
string r = "{";
for (const auto& x : v) {
if (!f) r += ", ";
f = 0;
r += to_string(x);
}
return r + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int inf = 1e9;
const long long INF = 1e18;
const int mod = 1e9 + 7;
const int bit32 = log2(inf) + 3;
const int bit64 = log2(INF) + 3;
inline int add(int a, int b) {
a += b;
if (a >= mod) a -= mod;
return a;
}
inline int sub(int a, int b) {
a -= b;
if (a < 0) a += mod;
return a;
}
inline int mul(int a, int b) { return (int)((long long)a * b % mod); }
inline int modexpo(int a, int b) {
int res = 1;
while (b > 0) {
if (b & 1) res = mul(res, a);
a = mul(a, a);
b /= 2;
}
return res;
}
inline int divide(int a, int b) { return mul(a, modexpo(b, mod - 2)); }
clock_t time_p = clock();
void ktj() {
time_p = clock() - time_p;
cerr << "Time elapsed : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
const int N = 2e5 + 5;
vector<int> g[N];
bool vis[N];
void pre(int n) {
for (int i = 1; i <= n; i++) {
g[i].clear();
vis[i] = 0;
}
}
set<int> s, r;
void dfs(int u, int ignore) {
if (u == ignore) return;
if (vis[u]) return;
vis[u] = 1;
s.insert(u);
for (auto v : g[u]) {
dfs(v, ignore);
}
}
void solve() {
int n, m, a, b;
cin >> n >> m >> a >> b;
pre(n);
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(a, b);
for (int i = 1; i <= n; i++) {
vis[i] = 0;
}
r = s;
s.clear();
dfs(b, a);
for (int i = 1; i <= n; i++) {
if (s.count(i) and r.count(i)) {
s.erase(i);
r.erase(i);
}
}
s.erase(b);
r.erase(a);
cout << (int)s.size() * 1LL * (int)r.size() << '\n';
s.clear();
r.clear();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
}
ktj();
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b;
int vis[200010];
vector<int> g[200010];
int atual = 0;
int dfs(int i, int ignore) {
if (vis[i] == atual) return 0;
vis[i] = atual;
if (ignore) {
if (i == a) return 0;
} else if (i == b) {
return 0;
}
int ans = 1;
for (auto x : g[i]) {
ans += dfs(x, ignore);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
cin >> n >> m >> a >> b;
for (int i = 0; i <= n; i++) {
g[i].clear();
}
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
atual++;
long long ans1 = 0;
dfs(a, 0);
for (int i = 1; i <= n; i++) {
ans1 += (vis[i] != atual);
}
atual++;
cerr << "\033[31m"
<< "ans1"
<< " = " << ans1 << "\033[0m" << '\n';
;
long long ans2 = 0;
dfs(b, 1);
for (int i = 1; i <= n; i++) {
ans2 += (vis[i] != atual);
}
cerr << "\033[31m"
<< "ans2"
<< " = " << ans2 << "\033[0m" << '\n';
;
cout << ans1 * ans2 << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int cnt;
bool chk;
vector<int> temp;
void dfs(vector<int> g[], bool vis[], int u, int x) {
vis[u] = true;
cnt++;
temp.push_back(u);
if (u == x) {
chk = true;
}
for (auto v : g[u]) {
if (!vis[v]) {
dfs(g, vis, v, x);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, m, a, b;
cin >> n >> m >> a >> b;
bool vis[n + 1];
vector<int> g[n + 1];
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
memset(vis, false, sizeof(vis));
vis[a] = true;
long long int aset = 0;
vector<int> temp1;
for (auto u : g[a]) {
if (!vis[u]) {
cnt = 0;
chk = false;
temp.clear();
dfs(g, vis, u, b);
if (!chk) {
aset += cnt;
} else {
temp1 = temp;
}
}
}
for (auto item : temp1) {
vis[item] = false;
}
long long int bset = 0;
vis[b] = true;
vis[a] = false;
for (auto u : g[b]) {
if (!vis[u]) {
cnt = 0;
chk = false;
dfs(g, vis, u, a);
if (!chk) {
bset += cnt;
}
}
}
long long int ans = (aset) * (bset);
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
const long base = 151;
const long long MM = 1ll * 1000000007 * 1000000007;
using namespace std;
int BIT(int i, long long x) { return (x & (1 << i)); }
long long ONBIT(int i, long long x) { return (x | (1 << i)); }
long long OFFBIT(int i, long long x) { return (x & ~(1 << i)); }
long long FLIPBIT(int i, long long x) { return (x ^ (1 << i)); }
long long NUMBIT(long long x) { return __builtin_popcount(x); }
template <class T>
T GCD(T a, T b) {
T r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
template <class T>
T LCM(T a, T b) {
return a / GCD(a, b) * b;
}
long n, m, x, y;
vector<long> a[600005];
long va[600005], vb[600005];
long used[600005];
void read() {
cin >> n >> m >> x >> y;
for (long i = 1; i <= n; i++) a[i].clear(), used[i] = 0, va[i] = 0, vb[i] = 0;
for (long i = 1; i <= m; i++) {
long u, v;
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
}
bool av[600005];
void DFS(long u, long k) {
av[u] = true;
if (k == x && u == y) return;
if (k == y && u == x) return;
used[u]++;
if (k == x) va[u]++;
if (k == y) vb[u]++;
for (long i = 0; i < a[u].size(); i++) {
long v = a[u][i];
if (av[v] == false) DFS(v, k);
}
}
void solve() {
memset(av, 0, sizeof(av));
DFS(x, x);
memset(av, 0, sizeof(av));
DFS(y, y);
long long da = 0, db = 0;
for (long i = 1; i <= n; i++) {
if (used[i] != 2 && va[i] == 1 && i != x) da++;
if (used[i] != 2 && vb[i] == 1 && i != y) db++;
}
cout << da * db << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
long te;
cin >> te;
while (te--) {
read();
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
os << "[ ";
for (auto v : V) os << v << " ";
return os << "]";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
const long long int maxn = 2000001;
long long int sz[maxn], par[maxn];
void init(long long int n) {
for (long long int i = 1; i <= n; i++) {
sz[i] = 1;
par[i] = i;
}
}
long long int root(long long int x) {
if (par[x] == x)
return x;
else {
return (par[x] = root(par[x]));
}
}
void un(long long int u, long long int v) {
u = root(u);
v = root(v);
if (u == v) return;
if (sz[u] < sz[v]) swap(u, v);
par[v] = u;
sz[u] += sz[v];
return;
}
void solve() {
long long int n, m, a, b;
cin >> n >> m >> a >> b;
vector<pair<long long int, long long int> > e;
for (long long int i = 0; i < m; i++) {
long long int u, v;
cin >> u >> v;
e.push_back({u, v});
}
long long int x1 = 0, x2 = 0, x3 = 0;
init(n);
for (long long int i = 0; i < m; i++) {
if (e[i].first == a || e[i].second == a) continue;
un(e[i].first, e[i].second);
}
set<long long int> s1, s2;
for (long long int i = 1; i <= n; i++) {
if (root(i) == root(b)) {
s1.insert(i);
}
}
init(n);
for (long long int i = 0; i < m; i++) {
if (e[i].first == b || e[i].second == b) continue;
un(e[i].first, e[i].second);
}
for (long long int i = 1; i <= n; i++) {
if (root(i) == root(a)) {
s2.insert(i);
}
}
long long int ans = (n - s1.size() - 1) * (n - s2.size() - 1);
cout << ans << endl;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> v[1000050];
long long vis[1000050];
long long dfs(long long src) {
long long n = 0;
n++;
vis[src] = 1;
for (long long i = 0; i < v[src].size(); i++) {
if (!vis[v[src][i]]) {
n += dfs(v[src][i]);
}
}
return n;
}
signed main() {
long long t;
cin >> t;
while (t--) {
long long n, m, a, b, x, y, j, k;
cin >> n >> m >> a >> b;
for (long long i = 1; i <= n; i++) {
v[i].clear();
vis[i] = 0;
}
for (long long i = 0; i < m; i++) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
vis[b] = 1;
j = n - dfs(a);
for (long long i = 1; i <= n; i++) vis[i] = 0;
vis[a] = 1;
k = n - dfs(b);
cout << (j - 1) * (k - 1) << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
void find(vector<long long> graph[], long long vis[], long long start,
long long val) {
if (val == start) return;
vis[start] = 1;
long long i, size = graph[start].size(), x;
for (i = 0; i < size; i++) {
x = graph[start][i];
if (!vis[x]) {
vis[x] = 1;
find(graph, vis, x, val);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
long long t = 1;
cin >> t;
while (t--) {
long long n, m, a, b, i, x, y, val, cnt1 = 0, cnt2 = 0, ans = 1;
cin >> n >> m >> a >> b;
vector<long long> graph[n + 1];
long long vis[n + 1];
memset(vis, 0, sizeof(vis));
for (i = 0; i < m; i++) {
cin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
find(graph, vis, a, b);
for (i = 1; i <= n; i++)
if (!vis[i]) cnt2++;
memset(vis, 0, sizeof(vis));
find(graph, vis, b, a);
for (i = 1; i <= n; i++)
if (!vis[i]) cnt1++;
cout << cnt1 * cnt2 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-5;
const int N = 2e5 + 10;
void redirect() {
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
}
inline long long read() {
long long f = 1, x = 0;
char ch;
do {
ch = getchar();
if (ch == '-') f = -1;
} while (ch < '0' || ch > '9');
do {
x = x * 10 + ch - '0';
ch = getchar();
} while (ch >= '0' && ch <= '9');
return x * f;
}
struct node {
int pos, f;
node(int pos, int f) : pos(pos), f(f){};
};
queue<int> Q;
vector<int> G[N];
int n, m, s1, s2;
int vis[3][N], f[N];
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d%d%d%d", &n, &m, &s1, &s2);
for (int i = 1; i <= n; i++) G[i].clear();
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
memset(vis, 0, sizeof vis);
Q.push(s2);
vis[1][s2] = 1;
f[s2] = -1;
while (!Q.empty()) {
int t = Q.front();
Q.pop();
for (int i = 0; i < G[t].size(); i++) {
if (vis[1][G[t][i]] == 0 && G[t][i] != s1)
Q.push(G[t][i]), vis[1][G[t][i]] = 1, f[G[t][i]] = t;
}
}
Q.push(s1);
vis[2][s1] = 1;
f[s1] = -1;
while (!Q.empty()) {
int t = Q.front();
Q.pop();
for (int i = 0; i < G[t].size(); i++) {
if (vis[2][G[t][i]] == 0 && G[t][i] != s2)
Q.push(G[t][i]), vis[2][G[t][i]] = 1, f[G[t][i]] = t;
}
}
long long c1 = 0, c2 = 0, c3 = 0;
for (int i = 1; i <= n; i++) {
if (vis[1][i] == 0 && vis[2][i] == 1 && i != s1) c1++;
if (vis[1][i] == 1 && vis[2][i] == 0 && i != s2) c3++;
}
c2 = n - 2 - c1 - c3;
long long ans = c1 * c3;
printf("%lld\n", ans);
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200005;
int visited_a[MAX_N];
int visited_b[MAX_N];
vector<int> route[MAX_N];
int n, m, a, b;
int main() {
int test_case_num = 0;
cin >> test_case_num;
for (int test_case = 1; test_case <= test_case_num; test_case++) {
cin >> n >> m >> a >> b;
set<int> a_neighbor;
set<int> b_neighbor;
set<int> ab_neighbor;
for (int i = 0; i <= n; i++) {
route[i].clear();
}
for (int i = 0; i < m; i++) {
int temp1, temp2;
cin >> temp1 >> temp2;
route[temp1].push_back(temp2);
route[temp2].push_back(temp1);
}
{
queue<int> q;
q.push(a);
visited_a[a] = test_case;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (vector<int>::iterator it = route[cur].begin();
it != route[cur].end(); it++) {
if (*it != b && visited_a[*it] != test_case) {
visited_a[*it] = test_case;
q.push(*it);
a_neighbor.insert(*it);
}
}
}
}
{
queue<int> q;
q.push(b);
visited_b[b] = test_case;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (vector<int>::iterator it = route[cur].begin();
it != route[cur].end(); it++) {
if (*it != a && visited_b[*it] != test_case) {
visited_b[*it] = test_case;
q.push(*it);
b_neighbor.insert(*it);
}
}
}
}
for (set<int>::iterator it = a_neighbor.begin(); it != a_neighbor.end();
it++) {
if (b_neighbor.find(*it) != b_neighbor.end()) {
ab_neighbor.insert(*it);
}
}
cout << (unsigned long long)((unsigned long long)a_neighbor.size() -
(unsigned long long)ab_neighbor.size()) *
((unsigned long long)b_neighbor.size() -
(unsigned long long)ab_neighbor.size())
<< endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long int> adj[200001];
long long int vis[200001];
long long int co[200001][2];
long long int ind, x;
long long int dfs(long long int no) {
for (long long int j = 0; j < adj[no].size(); j++) {
long long int i = adj[no][j];
if (vis[i] == 1) {
continue;
}
if (i == x) {
continue;
}
vis[i] = 1;
co[i][ind]++;
dfs(i);
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n, m, aa, bb;
cin >> n >> m >> aa >> bb;
long long int ac, bc;
aa--;
bb--;
for (long long int i = 0; i < n; i++) {
adj[i].clear();
}
for (long long int i = 0; i < m; i++) {
cin >> ac >> bc;
adj[ac - 1].push_back(bc - 1);
adj[bc - 1].push_back(ac - 1);
}
for (long long int i = 0; i < n; i++) {
vis[i] = 0;
co[i][0] = 0;
co[i][1] = 0;
}
x = bb;
ind = 0;
vis[aa] = 1;
dfs(aa);
for (long long int i = 0; i < n; i++) {
vis[i] = 0;
}
x = aa;
ind = 1;
vis[bb] = 1;
dfs(bb);
long long int coo[2];
coo[0] = 0;
coo[1] = 0;
for (long long int i = 0; i < n; i++) {
if (co[i][0] == 1 and co[i][1] == 1) {
continue;
}
coo[0] += co[i][0];
coo[1] += co[i][1];
}
cout << coo[0] * coo[1] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline void read(int &n) {
n = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') {
n = (n << 3) + (n << 1) + ch - '0';
ch = getchar();
}
return;
}
const int N = 2e5 + 10;
int T, n, m, x, y, a, b;
bool l[N], r[N];
vector<int> f[N];
void dfs1(int now, int no) {
if (now == no) return;
if (l[now]) return;
l[now] = 1;
for (int i = 0; i < f[now].size(); i++) dfs1(f[now][i], no);
return;
}
void dfs2(int now, int no) {
if (now == no) return;
if (r[now]) return;
r[now] = 1;
for (int i = 0; i < f[now].size(); i++) dfs2(f[now][i], no);
return;
}
int main() {
read(T);
while (T--) {
read(n);
read(m);
read(a);
read(b);
memset(l, 0, sizeof(l));
memset(r, 0, sizeof(r));
for (int i = 1; i <= n; i++) f[i].clear();
while (m--) {
read(x);
read(y);
f[x].push_back(y);
f[y].push_back(x);
}
dfs1(a, b);
dfs2(b, a);
x = y = 0;
l[a] = r[a] = l[b] = r[b] = 0;
for (int i = 1; i <= n; i++) {
if (l[i] && !r[i]) x++;
if (r[i] && !l[i]) y++;
}
printf("%lld\n", x * 1ll * y);
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const double PI = acos(-1);
const long long MOD = 1000000007;
template <class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
using Graph = vector<vector<int>>;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int Q;
cin >> Q;
for (int q = 0; q < (int)(Q); q++) {
int N, M, a, b;
cin >> N >> M >> a >> b;
a--;
b--;
Graph G(N);
for (int i = 0; i < (int)(M); i++) {
int x, y;
cin >> x >> y;
x--;
y--;
G[x].emplace_back(y);
G[y].emplace_back(x);
}
vector<bool> check(N, false);
check[a] = true;
queue<int> que;
que.emplace(b);
while (!que.empty()) {
int v = que.front();
que.pop();
check[v] = true;
for (auto nv : G[v]) {
if (check[nv]) continue;
que.emplace(nv);
}
}
long long c1 = 0;
for (int i = 0; i < (int)(N); i++)
if (!check[i]) c1++;
fill((check).begin(), (check).end(), false);
check[b] = true;
que.emplace(a);
while (!que.empty()) {
int v = que.front();
que.pop();
check[v] = true;
for (auto nv : G[v]) {
if (check[nv]) continue;
que.emplace(nv);
}
}
long long c2 = 0;
for (int i = 0; i < (int)(N); i++)
if (!check[i]) c2++;
cout << c1 * c2 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool uin(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
return a < b ? (a = b, true) : false;
}
const long long maxn = 2 * (long long)1e5 + 1000;
long long t, n, m, a, b, o1, o2;
void bfs1(const vector<vector<long long> > &e1, vector<long long> &vis1) {
queue<long long> q;
q.push(b);
vis1[b] = 1;
while (!q.empty()) {
long long now = q.front();
q.pop();
for (auto it : e1[now]) {
if (vis1[it]) continue;
vis1[it] = 1;
q.push(it);
}
}
}
void bfs2(const vector<vector<long long> > &e2, vector<long long> &vis2) {
queue<long long> q;
q.push(a);
vis2[a] = 1;
while (!q.empty()) {
long long now = q.front();
q.pop();
for (auto it : e2[now]) {
if (vis2[it]) continue;
vis2[it] = 1;
q.push(it);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout << fixed;
cin >> t;
while (t--) {
bool flag = true;
cin >> n >> m >> a >> b;
vector<vector<long long> > e1(n);
vector<vector<long long> > e2(n);
--a;
--b;
vector<long long> vis1(n, 0);
vector<long long> vis2(n, 0);
set<long long> res1;
set<long long> res2;
for (int i = 0; i < (int)(m); ++i) {
cin >> o1 >> o2;
--o1;
--o2;
if (o1 != a && o2 != a) {
e1[o1].push_back(o2);
e1[o2].push_back(o1);
}
if (o1 != b && o2 != b) {
e2[o1].push_back(o2);
e2[o2].push_back(o1);
}
}
bfs1(e1, vis1);
for (int i = 0; i < (int)(n); ++i) {
if (!vis1[i]) res1.insert(i);
}
for (int i = 0; i < (int)(n); ++i) cerr << vis1[i] << " ";
cerr << endl;
bfs2(e2, vis2);
for (int i = 0; i < (int)(n); ++i) {
if (!vis2[i]) res2.insert(i);
}
for (int i = 0; i < (int)(n); ++i) cerr << vis2[i] << " ";
cerr << endl;
long long num1 = res1.size();
long long num2 = res2.size();
long long temp = (num1 - 1) * (num2 - 1);
cout << temp << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1000001;
const bool DEBUG = 1;
template <typename S, typename T>
ostream &operator<<(ostream &out, pair<S, T> const &p) {
out << '(' << p.first << ", " << p.second << ')';
return out;
}
template <typename T>
ostream &operator<<(ostream &out, set<T> const &v) {
for (auto i = v.begin(); i != v.end(); i++) out << (*i) << ' ';
return out;
}
template <typename T, typename V>
ostream &operator<<(ostream &out, map<T, V> const &v) {
for (auto i = v.begin(); i != v.end(); i++)
out << "\n" << (i->first) << ":" << (i->second);
return out;
}
template <typename T, typename V>
ostream &operator<<(ostream &out, unordered_map<T, V> const &v) {
for (auto i = v.begin(); i != v.end(); i++)
out << "\n" << (i->first) << ":" << (i->second);
return out;
}
template <typename T>
ostream &operator<<(ostream &out, multiset<T> const &v) {
for (auto i = v.begin(); i != v.end(); i++) out << (*i) << ' ';
return out;
}
template <typename T>
ostream &operator<<(ostream &out, unordered_set<T> const &v) {
for (auto i = v.begin(); i != v.end(); i++) out << (*i) << ' ';
return out;
}
template <typename T>
ostream &operator<<(ostream &out, unordered_multiset<T> const &v) {
for (auto i = v.begin(); i != v.end(); i++) out << (*i) << ' ';
return out;
}
template <typename T>
ostream &operator<<(ostream &out, vector<T> const &v) {
long long l = v.size();
for (long long i = 0; i < l - 1; i++) out << v[i] << ' ';
if (l > 0) out << v[l - 1];
return out;
}
template <typename T>
void trace(const char *name, T &&arg1) {
cout << name << ":" << arg1 << "\n";
}
template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cout.write(names, comma - names) << ":" << arg1 << "|";
trace(comma + 1, args...);
}
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long cnt = 0;
long long visit[N];
void dfs(long long x, map<long long, set<long long>> &g,
map<long long, vector<long long>> &memo, long long t) {
if (visit[x]) {
return;
}
visit[x] = 1;
if (x != t) {
memo[x].push_back(t);
}
for (auto p : g[x]) {
if (!visit[p]) {
dfs(p, g, memo, t);
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long _t;
cin >> _t;
while (_t--) {
long long n, m, a, b;
cin >> n >> m >> a >> b;
map<long long, set<long long>> g, g1;
cnt = 0;
for (long long i = 0; i <= n; i++) {
visit[i] = 0;
}
for (long long i = 0; i < m; i++) {
long long x, y;
cin >> x >> y;
g[x].insert(y);
g[y].insert(x);
}
map<long long, vector<long long>> memo;
visit[b] = 1;
long long p = a;
dfs(a, g, memo, p);
for (long long i = 0; i <= n; i++) {
visit[i] = 0;
}
visit[a] = 1;
p = b;
dfs(b, g, memo, p);
long long cnt1 = 0, cnt2 = 0;
for (auto p : memo) {
if (p.second.size() == 1) {
if (p.second[0] == a) {
cnt1++;
} else {
cnt2++;
}
}
}
long long ans = cnt1 * cnt2;
cout << ans << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int q, num;
vector<int> v[maxn];
bool vis[maxn];
void dfs(int x) {
for (int i = 0; i < v[x].size(); i++) {
if (!vis[v[x][i]]) {
num++;
vis[v[x][i]] = 1;
dfs(v[x][i]);
}
}
}
int main() {
cin >> q;
while (q--) {
int n, m, a, b;
long long ans;
cin >> n >> m >> a >> b;
for (int i = 1; i <= n; i++) {
v[i].clear();
vis[i] = 0;
}
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
vis[b] = vis[a] = 1, num = 0;
dfs(a);
ans = n - 2 - num;
fill(vis + 1, vis + 1 + n, 0);
vis[b] = vis[a] = 1, num = 0;
dfs(b);
ans *= (n - 2) - num;
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long long MOD = 1e+9 + 7;
const int INFi = 0x7f7f7f7f;
const long long MAXN = 2e+5 + 7;
vector<long long> adj[MAXN];
long long visit[MAXN] = {};
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};
int qq;
long long n = 0, m, a, b, x, y;
long long ct = 0;
void dfs(long long child, long long no) {
visit[child] = 1;
ct++;
for (auto it : adj[child]) {
if (!visit[it] && it != no) dfs(it, no);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed;
cout << setprecision(10);
;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
;
cin >> qq;
while (qq--) {
long long cnt1 = 0, cnt2 = 0;
for (long long i = (1); i <= n; i++) adj[i].clear();
cin >> n >> m >> a >> b;
for (long long i = 0; i < m; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
fill_n(visit, n + 1, 0);
dfs(a, b);
cnt1 = n - ct - 1;
ct = 0;
fill_n(visit, n + 1, 0);
dfs(b, a);
cnt2 = n - ct - 1;
ct = 0;
cout << cnt1 * cnt2;
cout << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> ar[500004 + 2];
int coutn(int s, int bad, int n) {
int vis[n + 2];
memset(vis, 0, sizeof vis);
queue<int> q;
q.push(s);
vis[s] = 1;
int res = 0;
while (!q.empty()) {
int f = q.front();
q.pop();
res++;
if (f == bad) continue;
for (int i = 0; i < ar[f].size(); i++) {
int x = ar[f][i];
if (vis[x]) continue;
vis[x] = 1;
q.push(x);
}
}
return res;
}
int main() {
int tc;
cin >> tc;
while (tc--) {
int n, m, a, b;
cin >> n >> m >> a >> b;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
ar[x].push_back(y);
ar[y].push_back(x);
}
long long x = n - coutn(a, b, n);
long long y = n - coutn(b, a, n);
cout << (x * y) << endl;
for (int i = 0; i <= n; i++) ar[i].clear();
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
int t, n, m, a, b, par1[N], par2[N], low[N], dfn[N], timer, cnt1, cnt2;
bool cut[N], vis[N], vis1[N], vis2[N], vis3[N], vis4[N];
vector<int> adj[N];
set<int> s;
int get1(int x) { return par1[x] == x ? x : par1[x] = get1(par1[x]); }
void mrg1(int x, int y) {
x = get1(x);
y = get1(y);
if (x == y) return;
if (rand() & 1) swap(x, y);
par1[x] = y;
}
int get2(int x) { return par2[x] == x ? x : par2[x] = get2(par2[x]); }
void mrg2(int x, int y) {
x = get2(x);
y = get2(y);
if (x == y) return;
if (rand() & 1) swap(x, y);
par2[x] = y;
}
void dfs(int u, int v) {
low[u] = dfn[u] = ++timer;
for (int x : adj[u]) {
if (x == v) continue;
if (!dfn[x]) {
dfs(x, u);
if (dfn[u] <= low[x]) cut[u] = 1;
low[u] = min(low[u], low[x]);
} else
low[u] = min(low[u], dfn[x]);
}
}
void _clear() {
timer = cnt1 = cnt2 = 0;
for (int i = 1; i <= n; i++) {
par1[i] = par2[i] = i;
low[i] = dfn[i] = cut[i] = vis[i] = vis1[i] = vis2[i] = vis3[i] = vis4[i] =
0;
adj[i].clear();
}
}
void dfs1(int u, int p) {
vis1[u] = 1;
mrg1(u, p);
for (auto x : adj[u])
if (!vis1[x] && x != a) dfs1(x, p);
}
void dfs2(int u, int p) {
vis2[u] = 1;
mrg2(u, p);
for (auto x : adj[u])
if (!vis2[x] && x != b) dfs2(x, p);
}
void dfscnt1(int u) {
vis3[u] = 1;
cnt1++;
for (auto x : adj[u])
if (!vis3[x] && x != a) dfscnt1(x);
}
void dfscnt2(int u) {
vis4[u] = 1;
cnt2++;
for (auto x : adj[u])
if (!vis4[x] && x != b) dfscnt2(x);
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d%d%d", &n, &m, &a, &b);
_clear();
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, 0);
for (auto x : adj[1]) s.insert(low[x]);
if ((int)(s.size()) != 1) cut[1] = 1;
if (!cut[a] || !cut[b]) {
printf("0\n");
continue;
}
for (auto x : adj[a])
if (!vis1[x]) dfs1(x, x);
for (auto x : adj[b])
if (!vis2[x]) dfs2(x, x);
for (auto x : adj[a]) {
int x1 = get1(x);
int x2 = get1(b);
if (x1 != x2 && !vis3[x]) dfscnt1(x);
}
for (auto x : adj[b]) {
int x1 = get2(x);
int x2 = get2(a);
if (x1 != x2 && !vis4[x]) dfscnt2(x);
}
printf("%lld\n", 1LL * cnt1 * cnt2);
}
}
|
#include <bits/stdc++.h>
inline long long read() {
long long x = 0, f = 1;
char c = getchar();
for (; c > '9' || c < '0'; c = getchar()) {
if (c == '-') {
f = -1;
}
}
for (; c >= '0' && c <= '9'; c = getchar()) {
x = x * 10 + c - '0';
}
return x * f;
}
struct Edge {
int to, next;
} edge[1000005];
bool flag[2][200005];
int head[200005], cnte, tot1, tot2;
inline void insert(int u, int v) {
edge[++cnte] = (Edge){v, head[u]}, head[u] = cnte;
edge[++cnte] = (Edge){u, head[v]}, head[v] = cnte;
}
void dfs(int now, int kind) {
flag[kind][now] = 1;
for (int i = head[now]; i; i = edge[i].next) {
if (!flag[kind][edge[i].to]) dfs(edge[i].to, kind);
}
}
inline void work() {
for (int t = read(); t; t--) {
int n = read(), m = read(), a = read(), b = read();
memset(head, 0, (n + 1) * sizeof(int));
memset(flag[0], 0, (n + 1) * sizeof(bool));
memset(flag[1], 0, (n + 1) * sizeof(bool));
cnte = 0, tot1 = 0, tot2 = 0;
for (int i = 1; i <= m; i++) {
int x = read(), y = read();
insert(x, y);
}
flag[0][a] = 1;
dfs(b, 0);
flag[1][b] = 1;
dfs(a, 1);
for (int i = 1; i <= n; i++) {
if (!flag[0][i]) tot1++;
if (!flag[1][i]) tot2++;
}
printf("%I64d\n", 1ll * tot1 * tot2);
}
}
int main() {
work();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MX = 200002;
vector<vector<int>> g;
bool vis[MX];
int dfs(int u, int b) {
vis[u] = 1;
int cnt = 0;
for (int i = 0; i < g[u].size(); i++) {
if (!vis[g[u][i]] && g[u][i] != b) cnt += dfs(g[u][i], b);
}
return cnt + 1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, m, a, b;
cin >> n >> m >> a >> b;
g.resize(n + 1);
--a;
--b;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
fill(vis, vis + n, 0);
long long ans1 = n - 1 - dfs(a, b);
fill(vis, vis + n, 0);
long long ans2 = n - 1 - dfs(b, a);
cout << ans1 * ans2 << "\n";
g.clear();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void swap(long long &a, long long &b) {
auto tm = a;
a = b;
b = tm;
}
const long long mod = 1000000009;
const long long mod2 = 998244353;
const long long INF = (1LL << 20) - 1;
const long long N = 2e5 + 25;
long long n, m, a, b;
vector<long long> v[N];
set<long long> c, d;
bool vt[N];
void dfs1(long long x) {
vt[x] = 1;
for (auto &(i) : (v[x])) {
if (i == b) {
c.insert(x);
continue;
}
if (vt[i] == 0) dfs1(i);
}
}
void dfs2(long long x) {
vt[x] = 1;
for (auto &(i) : (v[x])) {
if (i == a) {
d.insert(x);
continue;
}
if (vt[i] == 0) dfs2(i);
}
}
void bheja_fry() {
c.clear();
d.clear();
cin >> n >> m >> a >> b;
for (long long(i) = (0); (i) < (n + 1); ++(i)) {
v[i].clear();
vt[i] = 0;
}
long long x, y;
for (long long(i) = (0); (i) < (m); ++(i)) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
long long ct1 = 0, ct2 = 0;
dfs1(a);
for (long long(i) = (0); (i) < (n + 1); ++(i)) vt[i] = 0;
dfs2(b);
queue<long long> q;
for (auto &(i) : (v[b])) {
if (c.find(i) == c.end()) q.push(i);
}
for (long long(i) = (0); (i) < (n + 1); ++(i)) vt[i] = 0;
vt[b] = 1;
while (!q.empty()) {
x = q.front();
q.pop();
if (vt[x]) continue;
ct1++;
vt[x] = 1;
for (auto &(i) : (v[x])) {
if (vt[i] == 0) q.push(i);
}
}
for (auto &(i) : (v[a])) {
if (d.find(i) == d.end()) q.push(i);
}
for (long long(i) = (0); (i) < (n + 1); ++(i)) vt[i] = 0;
vt[a] = 1;
while (!q.empty()) {
x = q.front();
q.pop();
if (vt[x]) continue;
ct2++;
vt[x] = 1;
for (auto &(i) : (v[x])) {
if (vt[i] == 0) q.push(i);
}
}
cout << ct1 * ct2;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
cin >> t;
for (long long(i) = (1); (i) < (t + 1); ++(i)) {
bheja_fry();
if (i < t) cout << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v[200005];
int poz[200005];
bool viz[200005];
int a, b, n;
void reset() {
for (int i = 0; i <= n; i++) {
v[i].clear();
viz[i] = 0;
poz[i] = 0;
}
}
void dfs(int nod, int val) {
if (val == 1 && nod == b) return;
if (val == -1 && nod == a) return;
viz[nod] = 1;
poz[nod] += val;
for (int i = 0; i < v[nod].size(); i++) {
if (viz[v[nod][i]] == 0) dfs(v[nod][i], val);
}
}
int main() {
int t, m, x, y;
cin >> t;
while (t--) {
cin >> n >> m >> a >> b;
reset();
for (int i = 1; i <= m; i++) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(a, 1);
memset(viz, 0, sizeof(viz));
dfs(b, -1);
poz[a] = 2;
poz[b] = 2;
int cnt1 = 0, cnt2 = 0;
for (int i = 1; i <= n; i++) {
if (poz[i] == -1)
cnt2++;
else {
if (poz[i] == 1) cnt1++;
}
}
cout << 1ll * cnt1 * cnt2 << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-10;
using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
void dfs(int s, int u, int a, int b, vector<vector<int>> &G, set<int> &k,
vector<bool> &visited) {
k.insert(u);
visited[u] = true;
if (s == a && u == b) return;
if (s == b && u == a) return;
for (int v : G[u]) {
if (!visited[v]) dfs(s, v, a, b, G, k, visited);
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n, m, a, b;
cin >> n >> m >> a >> b;
a--;
b--;
vector<vector<int>> G(n);
for (int i = (int)(0); i < (int)(m); i++) {
int u, v;
cin >> u >> v;
u--;
v--;
G[u].push_back(v);
G[v].push_back(u);
}
set<int> k1, k2;
vector<bool> visited1(n, false), visited2(n, false);
dfs(a, a, a, b, G, k1, visited1);
dfs(b, b, a, b, G, k2, visited2);
ll ans1 = n - k1.size(), ans2 = n - k2.size();
cout << ans1 * ans2 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const long long MOD = 1e9 + 7;
int N, M, Q;
vector<int> graph[MAXN];
int dp[MAXN];
int vis[MAXN];
int a, b;
int go(int u, int target) {
int& ret = dp[u];
if (ret != -1) return ret;
if (u == target) return dp[u] = 1;
vis[u] = 1;
ret = 0;
for (int i = 0; i < graph[u].size(); ++i) {
int v = graph[u][i];
if (vis[v]) continue;
ret = ((ret) > (go(v, target)) ? (ret) : (go(v, target)));
}
vis[u] = 0;
return ret;
}
void solve(int t) {
int i, j, u, v;
scanf("%d%d%d%d", &N, &M, &a, &b);
for (i = 1; i <= N; ++i) {
graph[i].clear();
dp[i] = -1;
}
for (i = 1; i <= M; ++i) {
scanf("%d%d", &u, &v);
graph[u].push_back(v);
graph[v].push_back(u);
}
go(a, b);
long long cnt1 = 0;
long long cnt2 = 0;
for (i = 1; i <= N; ++i) {
if (dp[i] == -1) ++cnt1;
}
for (i = 1; i <= N; ++i) dp[i] = -1;
go(b, a);
for (i = 1; i <= N; ++i) {
if (dp[i] == -1) ++cnt2;
}
long long tot = cnt1 * cnt2;
printf("%lld\n", tot);
return;
}
int main() {
int T;
scanf("%d", &T);
for (int t = 1; t <= T; ++t) {
solve(t);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 2e9 + 1;
const long long MOD = 998244353;
const long long MAXN = 5e3 + 100;
const long double EPS = 1e-12;
const long double PI = acos(-1);
const long long XX[] = {0, 0, 1, 1, 1, -1, -1, -1};
const long long YY[] = {1, -1, 1, -1, 0, 1, -1, 0};
const long long ALPH = 31;
const long long LEN = 20;
mt19937 gen(228);
void bye() {
cout << "\n";
exit(0);
}
struct Solution {
int n, m;
vector<vector<int> > g;
vector<bool> used;
void dfs(int v) {
used[v] = 1;
for (auto to : g[v]) {
if (!used[to]) {
dfs(to);
}
}
}
void solve() {
cin >> n >> m;
int a, b;
cin >> a >> b;
a--, b--;
g.resize(n);
used.resize(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
long long left = 0, right = 0;
used[b] = 1;
dfs(a);
for (auto v : used) {
left += !v;
}
fill((used).begin(), used.end(), 0);
used[a] = 1;
dfs(b);
for (auto v : used) {
right += !v;
}
cout << left * right << "\n";
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long TEST_COUNT = 1;
cin >> TEST_COUNT;
while (TEST_COUNT--) {
Solution kek;
kek.solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MAX = 2e5 + 6;
vector<long long> v[MAX];
bool vis[MAX];
long long a, b;
void dfs(long long x) {
vis[x]++;
if (x == b) return;
for (auto i : v[x]) {
if (vis[i] == 0) dfs(i);
}
}
void dfs1(long long x) {
vis[x]++;
if (x == a) return;
for (auto i : v[x]) {
if (vis[i] == 0) dfs1(i);
}
}
int main() {
long long t;
cin >> t;
while (t--) {
long long n, m, x, y;
cin >> n >> m >> a >> b;
for (int i = 1; i <= n; i++) v[i].clear();
while (m--) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for (int i = 1; i <= n; i++) vis[i] = 0;
dfs(a);
x = 0;
y = 0;
for (int i = 1; i <= n; i++) {
if (vis[i] == 0) x++;
vis[i] = 0;
}
dfs1(b);
for (int i = 1; i <= n; i++) {
if (vis[i] == 0) y++;
}
cout << x * y << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
struct DSU {
int par[MAXN], siz[MAXN];
;
void init(int n) {
for (int i = 0; i <= n; ++i) {
par[i] = i;
siz[i] = 1;
}
}
int Find(int x) {
if (par[x] != x) {
par[x] = Find(par[x]);
}
return par[x];
}
void Union(int x, int y) {
int xRt = Find(x), yRt = Find(y);
if (xRt == yRt) {
return;
}
if (siz[xRt] > siz[yRt]) {
swap(xRt, yRt);
}
par[xRt] = yRt;
siz[yRt] += siz[xRt];
}
};
int n, m;
DSU dsu;
int a, b;
unordered_map<int, int> bit;
vector<int> ver[3];
int vis[MAXN];
void init() {
dsu.init(n);
ver[1].clear();
ver[2].clear();
bit.clear();
bit[a] = 1;
bit[b] = 2;
memset(vis, 0, sizeof(int) * (n + 5));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
cin >> n >> m >> a >> b;
init();
while (m--) {
int u, v;
cin >> u >> v;
if (u == a && v == b || u == b && v == a) {
continue;
}
if (u == a || u == b) {
ver[bit[u]].push_back(v);
} else if (v == a || v == b) {
ver[bit[v]].push_back(u);
} else {
dsu.Union(u, v);
}
}
for (int i = 1; i <= 2; ++i) {
for (auto it : ver[i]) {
vis[dsu.Find(it)] |= i;
}
}
int ans[4] = {};
for (int i = 1; i <= n; i++) {
if (vis[dsu.Find(i)] != -1) {
ans[vis[dsu.Find(i)]] += dsu.siz[dsu.Find(i)];
vis[dsu.Find(i)] = -1;
}
}
cout << 1LL * ans[1] * ans[2] << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 200005;
int n, m, a, b, vis[MAXN], rnd, l;
bool toa, tob;
vector<int> G[MAXN];
void dfs(int id) {
l++;
vis[id] = rnd;
for (auto x : G[id]) {
if (x == a)
toa = true;
else if (x == b)
tob = true;
if (x == a || x == b || vis[x] == rnd) continue;
dfs(x);
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
memset(vis, 0, sizeof(vis));
rnd = 0;
int t;
cin >> t;
while (t--) {
cin >> n >> m >> a >> b;
for (int i = 1; i <= n; ++i) G[i].clear();
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
rnd++;
ll cnta, cntb;
cnta = cntb = 0;
for (int i = 1; i <= n; ++i) {
if (vis[i] != rnd && i != a && i != b) {
toa = tob = false;
l = 0;
dfs(i);
if (toa && tob)
continue;
else if (toa)
cnta += l;
else if (tob)
cntb += l;
}
}
cout << cnta * cntb << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool visited1[200002];
bool visited2[200002];
int node1, node2, mark[200002];
vector<int> from_node1, from_node2, adj[200002];
void run_dfs1(int node) {
if (node == node2) return;
if (visited1[node]) return;
visited1[node] = true;
from_node1.push_back(node);
for (int x : adj[node]) run_dfs1(x);
}
void run_dfs2(int node) {
if (node == node1) return;
if (visited2[node]) return;
visited2[node] = true;
from_node2.push_back(node);
for (int x : adj[node]) run_dfs2(x);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int i, m, n, t, x, y;
long long int cnt1, cnt2;
cin >> t;
while (t--) {
cin >> n >> m >> node1 >> node2;
for (i = 1; i <= n; i++) {
adj[i].clear();
mark[i] = 0;
visited1[i] = false;
visited2[i] = false;
}
while (m--) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
from_node1.clear();
from_node2.clear();
run_dfs1(node1);
run_dfs2(node2);
cnt1 = cnt2 = 0LL;
for (int node : from_node1) ++mark[node];
for (int node : from_node2) ++mark[node];
for (int node : from_node1) {
if (mark[node] == 1) ++cnt1;
}
for (int node : from_node2) {
if (mark[node] == 1) ++cnt2;
}
cout << (cnt1 - 1LL) * (cnt2 - 1LL) << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
vector<long long> adj[200003];
vector<bool> visited;
long long a, b;
vector<long long> par1, par2;
void dfs_a(long long s, long long par) {
if (!visited[s]) {
visited[s] = true;
par1[s] = par;
for (auto x : adj[s]) {
dfs_a(x, par);
}
}
}
void dfs_b(long long s, long long par) {
if (!visited[s]) {
visited[s] = true;
par2[s] = par;
for (auto x : adj[s]) {
dfs_b(x, par);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, i, j, k, t, temp;
long long tc;
cin >> tc;
while (tc--) {
long long m;
cin >> n >> m >> a >> b;
visited.clear();
visited.resize(n + 1);
par1.resize(n + 1);
par2.resize(n + 1);
long long tot = 0;
tot = (n - 2) * (n - 3);
tot /= 2;
for (i = 1; i <= n; i++) {
adj[i].clear();
par1[i] = i;
par2[i] = i;
}
for (i = 1; i <= m; i++) {
cin >> j >> k;
adj[j].push_back(k);
adj[k].push_back(j);
}
for (i = 1; i <= n; i++) {
visited[i] = false;
}
visited[b] = true;
for (i = 1; i <= n; i++) {
if (!visited[i]) {
dfs_a(i, i);
}
}
for (i = 1; i <= n; i++) {
visited[i] = false;
}
visited[a] = true;
for (i = 1; i <= n; i++) {
if (!visited[i]) {
dfs_b(i, i);
}
}
map<pair<long long, long long>, long long> both;
map<long long, long long> just_a, just_b;
for (i = 1; i <= n; i++) {
if (i != a & i != b) {
just_a[par1[i]]++;
just_b[par2[i]]++;
both[{par1[i], par2[i]}]++;
}
}
long long ans_a = 0, ans_b = 0, ans_ab = 0;
for (auto x : just_a) {
ans_a += (x.second * (x.second - 1)) / 2;
}
for (auto x : just_b) {
ans_b += (x.second * (x.second - 1)) / 2;
}
for (auto x : both) {
ans_ab += (x.second * (x.second - 1)) / 2;
}
tot = tot - ans_a - ans_b + ans_ab;
cout << tot << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 200009;
long long n, m, a, b;
vector<long long> graph[N];
void dfs(long long cur, long long blocked, unordered_set<long long>& seen) {
seen.insert(cur);
for (long long child : graph[cur]) {
if (child != blocked && !seen.count(child)) {
dfs(child, blocked, seen);
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
while (t--) {
cin >> n >> m >> a >> b;
for (long long i = 1; i <= n; ++i) graph[i].clear();
unordered_set<long long> froma, fromb;
for (long long i = 0; i < m; ++i) {
long long u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
dfs(a, b, froma);
dfs(b, a, fromb);
long long x = 0;
for (long long fa : froma) {
if (!fromb.count(fa)) x++;
}
long long y = fromb.size() - (froma.size() - x);
cout << (x - 1) * (y - 1) << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, m, a, b;
vector<long long> v[1000006];
vector<bool> v1, v2;
void dfs1(long long node) {
v1[node] = true;
for (auto x : v[node]) {
if (v1[x]) continue;
dfs1(x);
}
return;
}
void dfs2(long long node) {
v2[node] = true;
for (auto x : v[node]) {
if (v2[x]) continue;
dfs2(x);
}
return;
}
int main() {
long long tc;
cin >> tc;
while (tc--) {
cin >> n >> m >> a >> b;
v1.clear();
v2.clear();
for (long long i = 0; i <= n + 5; i++) v[i].clear();
for (long long i = 0; i <= n + 5; i++) v1.push_back(false);
for (long long i = 0; i <= n + 5; i++) v2.push_back(false);
while (m--) {
long long x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
v1[b] = true;
dfs1(a);
v2[a] = true;
dfs2(b);
long long x1, x2, res;
x1 = x2 = 0LL;
for (long long i = 1; i <= n; i++) {
if (v1[i] && v2[i]) continue;
if (v1[i]) x1++;
if (v2[i]) x2++;
}
res = x1 * x2;
cout << res << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to;
edge *nxt;
} edges[500000 * 7 + 5];
edge *ncnt = &edges[0], *Adj[400000 + 5], *Adj2[400000 + 5];
int n, m, a, b, N;
int dfn[400000 + 5], low[400000 + 5], dcnt;
int stk[400000 + 5], siz[400000 + 5], trfa[400000 + 5];
void Init() {
ncnt = &edges[0];
for (int i = 1; i <= 2 * n; i++)
Adj[i] = Adj2[i] = 0, dfn[i] = low[i] = 0, trfa[i] = 0;
N = n, stk[0] = 0, dcnt = 0;
}
void AddEdge(int u, int v) {
edge *p = ++ncnt;
p->to = v;
p->nxt = Adj[u];
Adj[u] = p;
edge *q = ++ncnt;
q->to = u;
q->nxt = Adj[v];
Adj[v] = q;
}
void AddEdge2(int u, int v) {
edge *p = ++ncnt;
p->to = v;
p->nxt = Adj2[u];
Adj2[u] = p;
}
void DFS(int u, int fa) {
dfn[u] = low[u] = ++dcnt;
stk[++stk[0]] = u;
for (edge *p = Adj[u]; p != NULL; p = p->nxt) {
int v = p->to;
if (dfn[v] == 0) {
DFS(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
AddEdge2(u, ++N);
int fro;
do {
fro = stk[stk[0]--];
AddEdge2(N, fro);
} while (fro != v);
}
} else if (v != fa)
low[u] = min(low[u], dfn[v]);
}
}
void DFS2(int u) {
siz[u] = 0;
if (u <= n) siz[u] = 1;
for (edge *p = Adj2[u]; p != NULL; p = p->nxt) {
int v = p->to;
trfa[v] = u;
DFS2(v);
siz[u] += siz[v];
}
}
void Print() {
for (int i = 1; i <= N; i++) {
printf("%d: ", i);
for (edge *p = Adj2[i]; p != NULL; p = p->nxt) printf("%d ", p->to);
printf("\n");
}
}
int main() {
int T;
scanf("%d", &T);
for (int tmn = 1; tmn <= T; tmn++) {
scanf("%d %d %d %d", &n, &m, &a, &b);
Init();
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d %d", &u, &v);
AddEdge(u, v);
}
DFS(a, -1);
int cnt = 0;
for (edge *p = Adj2[a]; p != NULL; p = p->nxt)
if (p->to > n) cnt++;
if (cnt == 1 || Adj[b] == NULL) {
printf("0\n");
continue;
}
DFS2(a);
int pos = b;
while (trfa[pos] != a) pos = trfa[pos];
printf("%lld\n", (1LL * siz[a] - 1LL * siz[pos] - 1LL) * (siz[b] - 1LL));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
for (T& x : v) is >> x;
return is;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
if (!v.empty()) {
os << v.front();
for (int x = 1; x < v.size(); ++x) os << ' ' << v[x];
}
return os;
}
void gg() {
cout << "-1\n";
exit(0);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
vector<vector<vector<int>>> ans(n,
vector<vector<int>>(m, vector<int>(k, -1)));
vector<vector<int>> xp(m, vector<int>(k)), xn(m, vector<int>(k)),
yp(n, vector<int>(k)), yn(n, vector<int>(k)), zp(n, vector<int>(m)),
zn(n, vector<int>(m)), pxp(m, vector<int>(k)),
pxn(m, vector<int>(k, n - 1)), pyp(n, vector<int>(k)),
pyn(n, vector<int>(k, m - 1)), pzp(n, vector<int>(m)),
pzn(n, vector<int>(m, k - 1));
cin >> xp >> xn >> yp >> yn >> zp >> zn;
queue<tuple<int, int, int>> q;
for (int x = 0; x < n; ++x)
for (int y = 0; y < m; ++y)
for (int z = 0; z < k; ++z)
if (!(xp[y][z] && xn[y][z] && yp[x][z] && yn[x][z] && zp[x][y] &&
zn[x][y]))
ans[x][y][z] = 0;
function<void(int, int)> exx = [&](int y, int z) {
while (pxp[y][z] < n && !ans[pxp[y][z]][y][z]) ++pxp[y][z];
while (pxn[y][z] >= 0 && !ans[pxn[y][z]][y][z]) --pxn[y][z];
if (pxp[y][z] >= n || pxn[y][z] < 0) gg();
q.emplace(pxp[y][z], y, z);
q.emplace(pxn[y][z], y, z);
};
function<void(int, int)> exy = [&](int x, int z) {
while (pyp[x][z] < m && !ans[x][pyp[x][z]][z]) ++pyp[x][z];
while (pyn[x][z] >= 0 && !ans[x][pyn[x][z]][z]) --pyn[x][z];
if (pyp[x][z] >= m || pyn[x][z] < 0) gg();
q.emplace(x, pyp[x][z], z);
q.emplace(x, pyn[x][z], z);
};
function<void(int, int)> exz = [&](int x, int y) {
while (pzp[x][y] < k && !ans[x][y][pzp[x][y]]) ++pzp[x][y];
while (pzn[x][y] >= 0 && !ans[x][y][pzn[x][y]]) --pzn[x][y];
if (pzp[x][y] >= k || pzn[x][y] < 0) gg();
q.emplace(x, y, pzp[x][y]);
q.emplace(x, y, pzn[x][y]);
};
for (int y = 0; y < m; ++y)
for (int z = 0; z < k; ++z) {
if (bool(xp[y][z]) != bool(xn[y][z])) gg();
if (!xp[y][z]) continue;
exx(y, z);
}
for (int x = 0; x < n; ++x)
for (int z = 0; z < k; ++z) {
if (bool(yp[x][z]) != bool(yn[x][z])) gg();
if (!yp[x][z]) continue;
exy(x, z);
}
for (int x = 0; x < n; ++x)
for (int y = 0; y < m; ++y) {
if (bool(zp[x][y]) != bool(zn[x][y])) gg();
if (!zp[x][y]) continue;
exz(x, y);
}
while (!q.empty()) {
int x, y, z;
tie(x, y, z) = q.front();
q.pop();
if (ans[x][y][z] == 0) continue;
vector<int> col;
if (pxp[y][z] == x) col.push_back(xp[y][z]);
if (pxn[y][z] == x) col.push_back(xn[y][z]);
if (pyp[x][z] == y) col.push_back(yp[x][z]);
if (pyn[x][z] == y) col.push_back(yn[x][z]);
if (pzp[x][y] == z) col.push_back(zp[x][y]);
if (pzn[x][y] == z) col.push_back(zn[x][y]);
if (col.size() == 0) continue;
col.erase(unique(col.begin(), col.end()), col.end());
if (col.size() == 1) {
ans[x][y][z] = col.back();
continue;
}
ans[x][y][z] = 0;
exx(y, z);
exy(x, z);
exz(x, y);
}
for (int x = 0; x < n; ++x) {
for (int y = 0; y < m; ++y) {
for (int z = 0; z < k; ++z)
if (ans[x][y][z] == -1) ans[x][y][z] = 0;
cout << ans[x][y] << '\n';
}
cout << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int T = 2e5 + 5;
int n, m, h;
int dx[6] = {1, -1, 0, 0, 0, 0};
int dy[6] = {0, 0, 1, -1, 0, 0};
int dz[6] = {0, 0, 0, 0, 1, -1};
struct Data {
int x, y, z;
int num;
int dir;
};
vector<Data> L;
queue<int> q;
vector<int> v[T];
int ans[T], vis[T];
int Calc(int x, int y, int z) { return (x - 1) * m * h + (y - 1) * h + z; }
bool judge(int p) {
int num = -1;
for (int i = 0; i < v[p].size(); i++)
if (num == -1)
num = L[v[p][i]].num;
else if (num == L[v[p][i]].num)
continue;
else
return 0;
if (num == 0)
return 0;
else
return 1;
}
bool Judge(int x, int y) {
if (x == 0 && y == 1) return 0;
if (x == 1 && y == 0) return 0;
if (x == 2 && y == 3) return 0;
if (x == 3 && y == 2) return 0;
if (x == 4 && y == 5) return 0;
if (x == 5 && y == 4) return 0;
return 1;
}
bool BFS() {
while (!q.empty()) {
int id = q.front();
int x = L[id].x, y = L[id].y, z = L[id].z, num = L[id].num, dir = L[id].dir;
q.pop();
int p = Calc(x, y, z);
if (num == 0 || vis[p] == 1) {
for (int i = 0; i < v[p].size(); i++) {
int d = v[p][i];
int xx = x + dx[L[d].dir], yy = y + dy[L[d].dir], zz = z + dz[L[d].dir];
if (xx > n || xx < 1 || yy > m || yy < 1 || zz > h || zz < 1)
if (L[d].num != 0)
return 0;
else
continue;
L[d].x = xx;
L[d].y = yy;
L[d].z = zz;
q.push(d);
v[Calc(xx, yy, zz)].push_back(d);
}
vis[p] = 1;
v[p].clear();
}
if (v[p].size() != 1)
if (judge(p) == 0) {
for (int i = 0; i < v[p].size(); i++) {
int d = v[Calc(x, y, z)][i];
int xx = x + dx[L[d].dir], yy = y + dy[L[d].dir],
zz = z + dz[L[d].dir];
if (xx > n || xx < 1 || yy > m || yy < 1 || zz > h || zz < 1)
if (L[d].num != 0)
return 0;
else
continue;
L[d].x = xx;
L[d].y = yy;
L[d].z = zz;
q.push(d);
v[Calc(xx, yy, zz)].push_back(d);
}
vis[p] = 1;
v[p].clear();
}
}
return 1;
}
int main() {
scanf("%d %d %d", &n, &m, &h);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= h; j++) {
int x;
scanf("%d", &x);
Data tmp = (Data){1, i, j, x, 0};
q.push((int)q.size());
L.push_back(tmp);
v[Calc(1, i, j)].push_back((int)L.size() - 1);
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= h; j++) {
int x;
scanf("%d", &x);
Data tmp = (Data){n, i, j, x, 1};
q.push((int)q.size());
L.push_back(tmp);
v[Calc(n, i, j)].push_back((int)L.size() - 1);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= h; j++) {
int x;
scanf("%d", &x);
Data tmp = (Data){i, 1, j, x, 2};
q.push((int)q.size());
L.push_back(tmp);
v[Calc(i, 1, j)].push_back((int)L.size() - 1);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= h; j++) {
int x;
scanf("%d", &x);
Data tmp = (Data){i, m, j, x, 3};
q.push((int)q.size());
L.push_back(tmp);
v[Calc(i, m, j)].push_back((int)L.size() - 1);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int x;
scanf("%d", &x);
Data tmp = (Data){i, j, 1, x, 4};
q.push((int)q.size());
L.push_back(tmp);
v[Calc(i, j, 1)].push_back((int)L.size() - 1);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int x;
scanf("%d", &x);
Data tmp = (Data){i, j, h, x, 5};
q.push((int)q.size());
L.push_back(tmp);
v[Calc(i, j, h)].push_back((int)L.size() - 1);
}
if (BFS() == 0) {
printf("-1");
return 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int k = 1; k <= h; k++) {
int p = Calc(i, j, k);
if (!v[p].empty()) ans[p] = L[v[p][0]].num;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int k = 1; k <= h; k++) printf("%d ", ans[Calc(i, j, k)]);
printf("\n");
}
printf("\n");
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int dx[] = {1, -1, 0, 0, 0, 0};
const int dy[] = {0, 0, 1, -1, 0, 0};
const int dz[] = {0, 0, 0, 0, 1, -1};
int n, m, K, x, a[N];
vector<int> vec[N];
bool IN(int x, int y, int z) {
return 1 <= x && x <= n && 1 <= y && y <= m && 1 <= z && z <= K;
}
int ID(int x, int y, int z) {
assert(IN(x, y, z));
return (x - 1) * m * K + (y - 1) * K + (z - 1);
}
void GG() {
puts("-1");
exit(0);
}
void erase(int, int, int);
void Insert(int, int, int, int, int);
bool flg = 0;
void insert(int d, int x, int y, int z, int c) {
if (!IN(x, y, z)) {
if (c) GG();
return;
}
int id = ID(x, y, z);
if (!c) {
if (a[id] > 0) erase(x, y, z);
a[id] = 0;
Insert(d, x, y, z, c);
} else if (a[id] == -1 || a[id] == c) {
a[id] = c;
vec[id].push_back(d);
} else if (!a[id])
Insert(d, x, y, z, c);
else {
erase(x, y, z);
a[id] = 0;
Insert(d, x, y, z, c);
}
}
void Insert(int d, int x, int y, int z, int c) {
insert(d, x + dx[d], y + dy[d], z + dz[d], c);
}
void erase(int x, int y, int z) {
int id = ID(x, y, z);
for (; vec[id].size();) {
int v = vec[id].back();
vec[id].pop_back();
Insert(v, x, y, z, a[id]);
}
}
int main() {
scanf("%d%d%d", &n, &m, &K);
memset(a, -1, sizeof(a));
for (int i = (int)(1); i <= (int)(m); i++)
for (int j = (int)(1); j <= (int)(K); j++)
scanf("%d", &x), insert(0, 1, i, j, x);
for (int i = (int)(1); i <= (int)(m); i++)
for (int j = (int)(1); j <= (int)(K); j++)
scanf("%d", &x), insert(1, n, i, j, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(K); j++)
scanf("%d", &x), insert(2, i, 1, j, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(K); j++)
scanf("%d", &x), insert(3, i, m, j, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(m); j++)
scanf("%d", &x), insert(4, i, j, 1, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(m); j++)
scanf("%d", &x), insert(5, i, j, K, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(m); j++)
for (int k = (int)(1); k <= (int)(K); k++) {
int ans = a[ID(i, j, k)];
printf("%d%c", max(ans, 0), k == K ? '\n' : ' ');
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = INT_MAX;
vector<int> dx = {1, -1, 0, 0, 0, 0};
vector<int> dy = {0, 0, 1, -1, 0, 0};
vector<int> dz = {0, 0, 0, 0, 1, -1};
struct cube {
int x, y, z;
int dir;
int col;
cube() {}
cube(int _x, int _y, int _z, int _dir, int _col) {
x = _x;
y = _y;
z = _z;
dir = _dir;
col = _col;
}
};
int main() {
queue<cube> q;
int n, m, k;
cin >> n >> m >> k;
int emp[n][m][k];
int col[n][m][k];
vector<cube> in[n][m][k];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int p = 0; p < k; p++) {
emp[i][j][p] = 0;
col[i][j][p] = -1;
in[i][j][p].clear();
}
}
}
for (int j = 0; j < m; j++) {
for (int p = 0; p < k; p++) {
cin >> col[0][j][p];
if (col[0][j][p] == 0) {
for (int i = 0; i < n; i++) {
emp[i][j][p] = 1;
}
} else {
q.push(cube(0, j, p, 0, col[0][j][p]));
}
}
}
for (int j = 0; j < m; j++) {
for (int p = 0; p < k; p++) {
cin >> col[n - 1][j][p];
if (col[n - 1][j][p] == 0) {
for (int i = 0; i < n; i++) {
emp[i][j][p] = 1;
}
} else {
q.push(cube(n - 1, j, p, 1, col[n - 1][j][p]));
}
}
}
for (int i = 0; i < n; i++) {
for (int p = 0; p < k; p++) {
cin >> col[i][0][p];
if (col[i][0][p] == 0) {
for (int j = 0; j < m; j++) {
emp[i][j][p] = 1;
}
} else {
q.push(cube(i, 0, p, 2, col[i][0][p]));
}
}
}
for (int i = 0; i < n; i++) {
for (int p = 0; p < k; p++) {
cin >> col[i][m - 1][p];
if (col[i][m - 1][p] == 0) {
for (int j = 0; j < m; j++) {
emp[i][j][p] = 1;
}
} else {
q.push(cube(i, m - 1, p, 3, col[i][m - 1][p]));
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> col[i][j][0];
if (col[i][j][0] == 0) {
for (int p = 0; p < k; p++) {
emp[i][j][p] = 1;
}
} else {
q.push(cube(i, j, 0, 4, col[i][j][0]));
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> col[i][j][k - 1];
if (col[i][j][k - 1] == 0) {
for (int p = 0; p < k; p++) {
emp[i][j][p] = 1;
}
} else {
q.push(cube(i, j, k - 1, 5, col[i][j][k - 1]));
}
}
}
int ans[n][m][k];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int p = 0; p < k; p++) {
ans[i][j][p] = -1;
}
}
}
while (!q.empty()) {
auto cur = q.front();
q.pop();
if (cur.x < 0 || cur.x >= n || cur.y < 0 || cur.y >= m || cur.z < 0 ||
cur.z >= k) {
cout << -1;
return 0;
}
if (emp[cur.x][cur.y][cur.z]) {
q.push(cube(cur.x + dx[cur.dir], cur.y + dy[cur.dir], cur.z + dz[cur.dir],
cur.dir, cur.col));
continue;
}
if (ans[cur.x][cur.y][cur.z] == -1) {
ans[cur.x][cur.y][cur.z] = cur.col;
in[cur.x][cur.y][cur.z].push_back(cur);
continue;
}
bool correct = true;
for (auto &f : in[cur.x][cur.y][cur.z]) {
if (f.col != cur.col) {
correct = false;
}
}
in[cur.x][cur.y][cur.z].push_back(cur);
if (correct) {
continue;
}
emp[cur.x][cur.y][cur.z] = 1;
ans[cur.x][cur.y][cur.z] = 0;
for (auto &f : in[cur.x][cur.y][cur.z]) {
q.push(cube(f.x + dx[f.dir], f.y + dy[f.dir], f.z + dz[f.dir], f.dir,
f.col));
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int p = 0; p < k; p++) {
if (ans[i][j][p] == -1) {
cout << 0 << " ";
continue;
}
cout << ans[i][j][p] << " ";
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> V2(int a, int b, int v) {
return vector<vector<int>>(a, vector<int>(b, v));
}
vector<vector<vector<int>>> V3(int a, int b, int c, int v) {
return vector<vector<vector<int>>>(a, V2(b, c, v));
}
void Read(vector<vector<int>>& x) {
int n = x.size(), m = x[0].size();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) cin >> x[i][j];
}
}
void Print(const vector<vector<vector<int>>>& x) {
int n = x.size(), m = x[0].size(), o = x[0][0].size();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
for (int k = 0; k < o; ++k) cout << x[i][j][k] << " ";
cout << '\n';
}
cout << '\n';
}
}
struct Point {
int a, b, c;
};
bool Inside(Point t, int n, int m, int k) {
return 0 <= t.a && t.a < n && 0 <= t.b && t.b < m && 0 <= t.c && t.c < k;
}
void UpdateColor(int& cc, int newc, bool& flag) {
if (cc == -1) {
cc = newc;
return;
}
if (cc != newc) flag = false;
cc = newc;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
auto x1 = V2(m, k, 0);
auto x2 = V2(m, k, 0);
auto y1 = V2(n, k, 0);
auto y2 = V2(n, k, 0);
auto z1 = V2(n, m, 0);
auto z2 = V2(n, m, 0);
Read(x1);
Read(x2);
Read(y1);
Read(y2);
Read(z1);
Read(z2);
auto ans = V3(n, m, k, -1);
auto state = V3(n, m, k, 0);
queue<Point> Q;
for (int a = 0; a < n; ++a) {
for (int b = 0; b < m; ++b) {
for (int c = 0; c < k; ++c) {
if (a == 0 || a == n - 1 || b == 0 || b == m - 1 || c == 0 ||
c == k - 1) {
Q.push({a, b, c});
int& st = state[a][b][c];
if (a == 0) st |= 1;
if (a == n - 1) st |= 2;
if (b == 0) st |= 4;
if (b == m - 1) st |= 8;
if (c == 0) st |= 16;
if (c == k - 1) st |= 32;
}
}
}
}
while (!Q.empty()) {
Point t = Q.front();
Q.pop();
int st = state[t.a][t.b][t.c];
int cc = -1;
bool flag = true;
if (st & 1) UpdateColor(cc, x1[t.b][t.c], flag);
if (st & 2) UpdateColor(cc, x2[t.b][t.c], flag);
if (st & 4) UpdateColor(cc, y1[t.a][t.c], flag);
if (st & 8) UpdateColor(cc, y2[t.a][t.c], flag);
if (st & 16) UpdateColor(cc, z1[t.a][t.b], flag);
if (st & 32) UpdateColor(cc, z2[t.a][t.b], flag);
ans[t.a][t.b][t.c] = cc;
if (flag && cc != 0) continue;
ans[t.a][t.b][t.c] = 0;
if ((st & 1) != 0 && (st & 2) == 0) {
Point s = t;
while (true) {
++s.a;
if (!Inside(s, n, m, k)) break;
if (state[s.a][s.b][s.c] & 1) break;
state[s.a][s.b][s.c] |= 1;
if (ans[s.a][s.b][s.c] == 0) continue;
Q.push(s);
break;
}
}
if ((st & 1) == 0 && (st & 2) != 0) {
Point s = t;
while (true) {
--s.a;
if (!Inside(s, n, m, k)) break;
if (state[s.a][s.b][s.c] & 2) break;
state[s.a][s.b][s.c] |= 2;
if (ans[s.a][s.b][s.c] == 0) continue;
Q.push(s);
break;
}
}
if ((st & 4) != 0 && (st & 8) == 0) {
Point s = t;
while (true) {
++s.b;
if (!Inside(s, n, m, k)) break;
if (state[s.a][s.b][s.c] & 4) break;
state[s.a][s.b][s.c] |= 4;
if (ans[s.a][s.b][s.c] == 0) continue;
Q.push(s);
break;
}
}
if ((st & 4) == 0 && (st & 8) != 0) {
Point s = t;
while (true) {
--s.b;
if (!Inside(s, n, m, k)) break;
if (state[s.a][s.b][s.c] & 8) break;
state[s.a][s.b][s.c] |= 8;
if (ans[s.a][s.b][s.c] == 0) continue;
Q.push(s);
break;
}
}
if ((st & 16) != 0 && (st & 32) == 0) {
Point s = t;
while (true) {
++s.c;
if (!Inside(s, n, m, k)) break;
if (state[s.a][s.b][s.c] & 16) break;
state[s.a][s.b][s.c] |= 16;
if (ans[s.a][s.b][s.c] == 0) continue;
Q.push(s);
break;
}
}
if ((st & 16) == 0 && (st & 32) != 0) {
Point s = t;
while (true) {
--s.c;
if (!Inside(s, n, m, k)) break;
if (state[s.a][s.b][s.c] & 32) break;
state[s.a][s.b][s.c] |= 32;
if (ans[s.a][s.b][s.c] == 0) continue;
Q.push(s);
break;
}
}
}
auto xx = V2(m, k, 0);
auto yy = V2(n, k, 0);
auto zz = V2(n, m, 0);
for (int a = 0; a < n; ++a) {
for (int b = 0; b < m; ++b) {
for (int c = 0; c < k; ++c) {
if (ans[a][b][c] == 0) continue;
if (ans[a][b][c] == -1) ans[a][b][c] = 1;
xx[b][c] = 1;
yy[a][c] = 1;
zz[a][b] = 1;
}
}
}
bool flag = true;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < k; ++j) {
if (xx[i][j] != 0) continue;
if (x1[i][j] != x2[i][j]) flag = false;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
if (yy[i][j] != 0) continue;
if (y1[i][j] != y2[i][j]) flag = false;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (zz[i][j] != 0) continue;
if (z1[i][j] != z2[i][j]) flag = false;
}
}
if (!flag) {
cout << -1 << endl;
return 0;
}
Print(ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct P {
int x, y, z;
};
queue<P> q;
inline void exi() {
puts("-1");
exit(0);
}
int n, m, k, i, j, l;
vector<vector<vector<int>>> a, inq, aa;
inline void inii(vector<vector<vector<int>>>& e0, int n, int m, int k) {
e0.resize(n);
for (auto& u : e0) {
u.resize(m);
for (auto& v : u) v.resize(k);
}
}
vector<vector<int>> e1, e2, e3, c0, c1, c2, c3, c4, c5, cov0, cov1, cov2;
inline void ini(vector<vector<int>>& cov, vector<vector<int>>& c0,
vector<vector<int>>& c1, int a, int b) {
for (i = 0; i < a; ++i)
for (j = 0; j < b; ++j) scanf("%d", &c0[i][j]);
for (i = 0; i < a; ++i)
for (j = 0; j < b; ++j) scanf("%d", &c1[i][j]);
for (i = 0; i < a; ++i)
for (j = 0; j < b; ++j) {
bool f0 = c0[i][j] == 0, f1 = c1[i][j] == 0;
if (f0 != f1) exi();
if (f0) cov[i][j] = 1;
}
}
struct aaa {
vector<vector<vector<int>>> q;
vector<vector<int>> h, t, c0, c1;
int n, m, k, tp;
void ini(int n, int m, int k, vector<vector<int>> zz, vector<vector<int>> c0,
vector<vector<int>> c1) {
inii(q, n, m, 0);
h = t = zz;
this->n = n;
this->m = m;
this->k = k;
this->c0 = c0;
this->c1 = c1;
}
void ini2() {
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
h[i][j] = 0, t[i][j] = int(q[i][j].size()) - 1;
}
int getv(int x, int y, int z) {
if (tp == 1) return a[z][x][y];
if (tp == 2) return a[x][z][y];
if (tp == 3) return a[x][y][z];
}
P getv2(int x, int y, int z) {
if (tp == 1) return (P){z, x, y};
if (tp == 2) return (P){x, z, y};
if (tp == 3) return (P){x, y, z};
}
void pop(int x, int y) {
for (; h[x][y] <= t[x][y];)
if (getv(x, y, q[x][y][h[x][y]]) == 0)
++h[x][y];
else if (getv(x, y, q[x][y][t[x][y]]) == 0)
--t[x][y];
else
break;
}
int ask(int x, int y, int z) {
pop(x, y);
if (h[x][y] == t[x][y]) {
if (c0[x][y] != c1[x][y]) exi();
return c0[x][y];
} else {
if (z == q[x][y][h[x][y]]) return c0[x][y];
if (z == q[x][y][t[x][y]]) return c1[x][y];
return -1;
}
}
void del(int x, int y, int z, vector<P>& ve) {
pop(x, y);
if (h[x][y] == t[x][y]) exi();
if (z == q[x][y][h[x][y]]) {
++h[x][y];
pop(x, y);
ve.push_back(getv2(x, y, q[x][y][h[x][y]]));
} else if (z == q[x][y][t[x][y]]) {
--t[x][y];
pop(x, y);
ve.push_back(getv2(x, y, q[x][y][t[x][y]]));
}
}
} a1, a2, a3;
bool upd(int& a, int b) {
if (a != -1 && b != -1 && a != b) return 0;
if (b != -1) a = b;
return 1;
}
void del(int x, int y, int z);
void trydel(int x, int y, int z) {
int& c = a[x][y][z];
if (!upd(c, a1.ask(y, z, x)) || !upd(c, a2.ask(x, z, y)) ||
!upd(c, a3.ask(x, y, z)))
del(x, y, z);
}
void del(int x, int y, int z) {
vector<P> ve;
a1.del(y, z, x, ve);
a2.del(x, z, y, ve);
a3.del(x, y, z, ve);
a[x][y][z] = 0;
for (auto u : ve)
if (a[u.x][u.y][u.z] != 0) trydel(u.x, u.y, u.z);
}
int main() {
scanf("%d%d%d", &n, &m, &k);
inii(a, n, m, k);
inii(inq, n, m, k);
e1.resize(m);
for (auto& u : e1) u.resize(k);
e2.resize(n);
for (auto& u : e2) u.resize(k);
e3.resize(n);
for (auto& u : e3) u.resize(m);
cov0 = c0 = c1 = e1;
ini(cov0, c0, c1, m, k);
cov1 = c2 = c3 = e2;
ini(cov1, c2, c3, n, k);
cov2 = c4 = c5 = e3;
ini(cov2, c4, c5, n, m);
a1.ini(m, k, n, e1, c0, c1);
a1.tp = 1;
a2.ini(n, k, m, e2, c2, c3);
a2.tp = 2;
a3.ini(n, m, k, e3, c4, c5);
a3.tp = 3;
for (i = 0; i < n; ++i)
for (j = 0; j < m; ++j)
for (l = 0; l < k; ++l) {
a[i][j][l] = cov0[j][l] || cov1[i][l] || cov2[i][j] ? 0 : -1;
if (a[i][j][l] == -1) {
a1.q[j][l].push_back(i);
a2.q[i][l].push_back(j);
a3.q[i][j].push_back(l);
}
}
a1.ini2();
a2.ini2();
a3.ini2();
for (i = 0; i < n; ++i)
for (j = 0; j < m; ++j)
for (l = 0; l < k; ++l)
if (a[i][j][l] != 0) trydel(i, j, l);
for (i = 0; i < n; ++i) {
for (j = 0; j < m; ++j, puts(""))
for (l = 0; l < k; ++l) {
if (a[i][j][l] == -1) a[i][j][l] = 1;
printf("%d ", a[i][j][l]);
}
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T1, typename T2>
inline void chkmin(T1 &x, const T2 &y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1 &x, const T2 &y) {
if (x < y) x = y;
}
int allocator_pos = 0;
char allocator_memory[(int)150 * 1024 * 1024];
inline void *operator new(size_t n) {
char *res = allocator_memory + allocator_pos;
allocator_pos += n;
assert(allocator_pos <= (int)150 * 1024 * 1024);
return (void *)res;
}
inline void operator delete(void *) noexcept {}
inline void operator delete(void *, size_t) {}
struct triple {
int x, y, z;
triple() {}
triple(int _x, int _y, int _z) { x = _x, y = _y, z = _z; }
};
int n, m, k;
vector<vector<int>> up, down, left228, right228, top, bottom;
void Read(vector<vector<int>> &have, int a, int b) {
have.resize(a, vector<int>(b));
for (auto &i : have)
for (auto &j : i) cin >> j;
}
void read() {
cin >> n >> m >> k;
Read(left228, m, k);
Read(right228, m, k);
Read(top, n, k);
Read(bottom, n, k);
Read(down, n, m);
Read(up, n, m);
}
bool operator==(const triple &a, const triple &b) {
return tie(a.x, a.y, a.z) == tie(b.x, b.y, b.z);
}
vector<vector<vector<triple>>> par[3];
vector<vector<vector<int>>> Min[3], Max[3];
triple get_par(triple a, int it) {
if (a == par[it][a.x][a.y][a.z]) return a;
return par[it][a.x][a.y][a.z] = get_par(par[it][a.x][a.y][a.z], it);
}
void uni(triple a, triple b, int it) {
a = get_par(a, it);
b = get_par(b, it);
if (a == b) return;
par[it][b.x][b.y][b.z] = a;
chkmin(Min[it][a.x][a.y][a.z], Min[it][b.x][b.y][b.z]);
chkmax(Max[it][a.x][a.y][a.z], Max[it][b.x][b.y][b.z]);
}
void no() {
cout << -1 << endl;
exit(0);
}
vector<vector<vector<bool>>> used;
queue<triple> q;
int get_color(int x, int y, int z) {
set<int> color;
triple a;
if (x != 0) {
a = {x - 1, y, z};
a = get_par(a, 0);
}
if (x == 0 || (Min[0][a.x][a.y][a.z] == 0 && used[x - 1][y][z])) {
color.insert(left228[y][z]);
}
if (x != n - 1) {
a = {x + 1, y, z};
a = get_par(a, 0);
}
if (x == n - 1 || (Max[0][a.x][a.y][a.z] == n - 1 && used[x + 1][y][z])) {
color.insert(right228[y][z]);
}
if (y != 0) {
a = {x, y - 1, z};
a = get_par(a, 1);
}
if (y == 0 || (Min[1][a.x][a.y][a.z] == 0 && used[x][y - 1][z])) {
color.insert(top[x][z]);
}
if (y != m - 1) {
a = {x, y + 1, z};
a = get_par(a, 1);
}
if (y == m - 1 || (Max[1][a.x][a.y][a.z] == m - 1 && used[x][y + 1][z])) {
color.insert(bottom[x][z]);
}
if (z != 0) {
a = {x, y, z - 1};
a = get_par(a, 2);
}
if (z == 0 || (Min[2][a.x][a.y][a.z] == 0 && used[x][y][z - 1])) {
color.insert(down[x][y]);
}
if (z != k - 1) {
a = {x, y, z + 1};
a = get_par(a, 2);
}
if (z == k - 1 || (Max[2][a.x][a.y][a.z] == k - 1 && used[x][y][z + 1])) {
color.insert(up[x][y]);
}
if (color.empty()) return -1;
if (color.size() >= 2) return 0;
return *color.begin();
}
void build() {
used.assign(n, vector<vector<bool>>(m, vector<bool>(k, false)));
for (int i = 0; i < 3; i++) {
par[i].resize(n, vector<vector<triple>>(m, vector<triple>(k)));
Min[i].resize(n, vector<vector<int>>(m, vector<int>(k)));
Max[i].resize(n, vector<vector<int>>(m, vector<int>(k)));
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
for (int it = 0; it < 3; it++) {
par[it][x][y][z] = {x, y, z};
}
Min[0][x][y][z] = x;
Max[0][x][y][z] = x;
Min[1][x][y][z] = y;
Max[1][x][y][z] = y;
Min[2][x][y][z] = z;
Max[2][x][y][z] = z;
}
}
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
if (!get_color(x, y, z)) {
q.push({x, y, z});
used[x][y][z] = true;
}
}
}
}
}
vector<int> dx = {-1, 1, 0, 0, 0, 0};
vector<int> dy = {0, 0, -1, 1, 0, 0};
vector<int> dz = {0, 0, 0, 0, -1, 1};
bool check(int x, int y, int z) {
return x >= 0 && x < n && y >= 0 && y < m && z >= 0 && z < k;
}
void del(int x, int y, int z) {
for (int i = 0; i < 6; i++) {
int nx = x + dx[i], ny = y + dy[i], nz = z + dz[i];
if (!check(nx, ny, nz)) continue;
if (!used[nx][ny][nz]) continue;
uni({x, y, z}, {nx, ny, nz}, i / 2);
}
}
void bfs() {
while (!q.empty()) {
auto v = q.front();
int x = v.x, y = v.y, z = v.z;
q.pop();
del(x, y, z);
for (int i = 0; i < 6; i++) {
int nx = x + dx[i], ny = y + dy[i], nz = z + dz[i];
if (!check(nx, ny, nz)) continue;
if (get_color(nx, ny, nz)) continue;
if (used[nx][ny][nz]) {
auto a = triple(nx, ny, nz);
a = get_par(a, i / 2);
if (i / 2 == 0) {
if (i % 2 == 0) {
nx = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
nx = Max[i / 2][a.x][a.y][a.z] + 1;
}
} else if (i / 2 == 1) {
if (i % 2 == 0) {
ny = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
ny = Max[i / 2][a.x][a.y][a.z] + 1;
}
} else {
if (i % 2 == 0) {
nz = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
nz = Max[i / 2][a.x][a.y][a.z] + 1;
}
}
if (!check(nx, ny, nz)) continue;
if (get_color(nx, ny, nz)) continue;
}
if (used[nx][ny][nz]) continue;
used[nx][ny][nz] = true;
q.push({nx, ny, nz});
}
}
}
void check_ans() {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
bool flag = false;
for (int x = 0; x < n; x++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (left228[y][z] || right228[y][z]) {
no();
}
}
}
}
for (int x = 0; x < n; x++) {
for (int z = 0; z < k; z++) {
bool flag = false;
for (int y = 0; y < m; y++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (top[x][z] || bottom[x][z]) {
no();
}
}
}
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
bool flag = false;
for (int z = 0; z < k; z++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (up[x][y] || down[x][y]) {
no();
}
}
}
}
}
void run() {
build();
bfs();
check_ans();
}
void write() {
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
int ans = get_color(x, y, z);
if (ans == -1) ans = 0;
if (used[x][y][z]) ans = 0;
cout << ans << " ";
}
}
}
cout << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
read();
run();
write();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int dx[] = {1, -1, 0, 0, 0, 0};
const int dy[] = {0, 0, 1, -1, 0, 0};
const int dz[] = {0, 0, 0, 0, 1, -1};
int n, m, K, x, a[N];
vector<int> vec[N];
bool IN(int x, int y, int z) {
return 1 <= x && x <= n && 1 <= y && y <= m && 1 <= z && z <= K;
}
int ID(int x, int y, int z) {
assert(IN(x, y, z));
return (x - 1) * m * K + (y - 1) * K + (z - 1);
}
void GG() {
puts("-1");
exit(0);
}
void erase(int, int, int);
void Insert(int, int, int, int, int);
bool flg = 0;
void insert(int d, int x, int y, int z, int c) {
if (!IN(x, y, z)) {
if (c) GG();
return;
}
int id = ID(x, y, z);
if (!c) {
if (a[id] > 0) erase(x, y, z);
a[id] = 0;
Insert(d, x, y, z, c);
} else if (a[id] == -1 || a[id] == c) {
a[id] = c;
vec[id].push_back(d);
} else if (!a[id])
Insert(d, x, y, z, c);
else {
erase(x, y, z);
a[id] = 0;
Insert(d, x, y, z, c);
}
}
void Insert(int d, int x, int y, int z, int c) {
insert(d, x + dx[d], y + dy[d], z + dz[d], c);
}
void erase(int x, int y, int z) {
int id = ID(x, y, z);
for (; vec[id].size();) {
int v = vec[id].back();
vec[id].pop_back();
Insert(v, x, y, z, a[id]);
}
}
int main() {
scanf("%d%d%d", &n, &m, &K);
memset(a, -1, sizeof(a));
for (int i = (int)(1); i <= (int)(m); i++)
for (int j = (int)(1); j <= (int)(K); j++)
scanf("%d", &x), insert(0, 1, i, j, x);
for (int i = (int)(1); i <= (int)(m); i++)
for (int j = (int)(1); j <= (int)(K); j++)
scanf("%d", &x), insert(1, n, i, j, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(K); j++)
scanf("%d", &x), insert(2, i, 1, j, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(K); j++)
scanf("%d", &x), insert(3, i, m, j, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(m); j++)
scanf("%d", &x), insert(4, i, j, 1, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(m); j++)
scanf("%d", &x), insert(5, i, j, K, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(m); j++)
for (int k = (int)(1); k <= (int)(K); k++) {
int ans = a[ID(i, j, k)];
printf("%d%c", max(ans, 0), k == K ? '\n' : ' ');
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using cat = long long;
template <typename T>
struct vec3 {
int N[3];
vector<T> data;
vec3() = default;
vec3(int N0, int N1, int N2, T val = 0) : N{N0, N1, N2} {
data.resize(N0 * N1 * N2, val);
}
T& operator()(int i0, int i1, int i2) {
return data[(i0 * N[1] + i1) * N[2] + i2];
}
};
template <typename T>
struct vec2 {
int N[2];
vector<T> data;
vec2() = default;
vec2(int N0, int N1, T val = 0) : N{N0, N1} { data.resize(N0 * N1, val); }
T& operator()(int i0, int i1) { return data[i0 * N[1] + i1]; }
};
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
int N[3];
for (int i = 0; i < 3; i++) cin >> N[i];
vec2<int> A[3][2];
int dim[3][2];
for (int k = 0; k < 3; k++)
for (int l = 0; l < 2; l++) {
dim[k][0] = k ? 0 : 1;
dim[k][1] = 3 - k - dim[k][0];
A[k][l] = vec2<int>(N[dim[k][0]], N[dim[k][1]]);
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++) cin >> A[k][l](i, j);
}
vec3<int> ans(N[0], N[1], N[2], -1);
for (int k = 0; k < 3; k++)
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++)
if (!A[k][0](i, j) || !A[k][1](i, j)) {
if (A[k][0](i, j) || A[k][1](i, j)) {
cout << "-1\n";
return 0;
}
int idx[3];
idx[dim[k][0]] = i;
idx[dim[k][1]] = j;
for (idx[k] = 0; idx[k] < N[k]; idx[k]++)
ans(idx[0], idx[1], idx[2]) = 0;
}
vec3<int> border[8];
for (int c = 0; c < 8; c++) border[c] = vec3<int>(N[0], N[1], N[2], 0);
vec3<int> cur[8];
for (int c = 0; c < 8; c++) cur[c] = vec3<int>(N[0], N[1], N[2], -1);
vec2<int> B[8][3];
for (int c = 0; c < 8; c++)
for (int k = 0; k < 3; k++) {
int x = (c >> k) & 1;
B[c][k] = vec2<int>(N[dim[k][0]], N[dim[k][1]], 0);
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++) {
int i_r = i, j_r = j;
if ((c >> dim[k][0]) & 1) i_r = N[dim[k][0]] - 1 - i;
if ((c >> dim[k][1]) & 1) j_r = N[dim[k][1]] - 1 - j;
B[c][k](i_r, j_r) = A[k][x](i, j);
}
}
for (int r = 0; r < min(N[0], min(N[1], N[2])); r++)
for (int c = 0; c < 8; c++) {
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
if (i == 0) border[c](i, j, k) |= 1;
if (j == 0) border[c](i, j, k) |= 2;
if (k == 0) border[c](i, j, k) |= 4;
int i_r = i, j_r = j, k_r = k;
if (c & 1) i_r = N[0] - 1 - i;
if (c & 2) j_r = N[1] - 1 - j;
if (c & 4) k_r = N[2] - 1 - k;
if (ans(i_r, j_r, k_r) == 0) cur[c](i, j, k) = 0;
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
int b = border[c](i, j, k);
if (!b) continue;
int idx[3] = {i, j, k};
if (cur[c](i, j, k) == -1)
for (int l = 0; l < 3; l++)
if ((b >> l) & 1) {
int val = B[c][l](idx[dim[l][0]], idx[dim[l][1]]);
if (cur[c](i, j, k) == -1)
cur[c](i, j, k) = val;
else if (cur[c](i, j, k) != val) {
cur[c](i, j, k) = 0;
break;
}
}
if (cur[c](i, j, k) == 0)
for (int l = 0; l < 3; l++) {
idx[l]++;
if (idx[l] != N[l])
border[c](idx[0], idx[1], idx[2]) |= b & (1 << l);
idx[l]--;
}
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
int i_r = i, j_r = j, k_r = k;
if (c & 1) i_r = N[0] - 1 - i;
if (c & 2) j_r = N[1] - 1 - j;
if (c & 4) k_r = N[2] - 1 - k;
int val_cur = cur[c](i, j, k), val_ans = ans(i_r, j_r, k_r);
if (val_cur != -1) {
if (val_ans != -1 && val_cur != val_ans)
ans(i_r, j_r, k_r) = 0;
else
ans(i_r, j_r, k_r) = val_cur;
}
}
}
for (int k = 0; k < 3; k++)
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++)
if (A[k][0](i, j)) {
int idx[3];
idx[dim[k][0]] = i;
idx[dim[k][1]] = j;
idx[k] = 0;
while (idx[k] < N[k] && !ans(idx[0], idx[1], idx[2])) idx[k]++;
if (idx[k] == N[k] || ans(idx[0], idx[1], idx[2]) != A[k][0](i, j)) {
cout << "-1\n";
return 0;
}
idx[k] = N[k] - 1;
while (idx[k] >= 0 && !ans(idx[0], idx[1], idx[2])) idx[k]--;
if (idx[k] == -1 || ans(idx[0], idx[1], idx[2]) != A[k][1](i, j)) {
cout << "-1\n";
return 0;
}
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++)
if (ans(i, j, k) == -1) ans(i, j, k) = 0;
for (int i = 0; i < N[0]; i++) {
if (i) cout << "\n";
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++)
cout << ans(i, j, k) << ((k == N[2] - 1) ? "\n" : " ");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using cat = long long;
template <typename T>
struct vec3 {
int N[3];
vector<T> data;
vec3() = default;
vec3(int N0, int N1, int N2, T val = 0) : N{N0, N1, N2} {
data.resize(N0 * N1 * N2, val);
}
T& operator()(int i0, int i1, int i2) {
return data[(i0 * N[1] + i1) * N[2] + i2];
}
};
template <typename T>
struct vec2 {
int N[2];
vector<T> data;
vec2() = default;
vec2(int N0, int N1, T val = 0) : N{N0, N1} { data.resize(N0 * N1, val); }
T& operator()(int i0, int i1) { return data[i0 * N[1] + i1]; }
};
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
int N[3];
for (int i = 0; i < 3; i++) cin >> N[i];
vec2<int> A[3][2];
int dim[3][2];
for (int k = 0; k < 3; k++)
for (int l = 0; l < 2; l++) {
dim[k][0] = k ? 0 : 1;
dim[k][1] = 3 - k - dim[k][0];
A[k][l] = vec2<int>(N[dim[k][0]], N[dim[k][1]]);
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++) cin >> A[k][l](i, j);
}
vec3<int> ans(N[0], N[1], N[2], -1);
for (int k = 0; k < 3; k++)
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++)
if (!A[k][0](i, j) || !A[k][1](i, j)) {
if (A[k][0](i, j) || A[k][1](i, j)) {
cout << "-1\n";
return 0;
}
int idx[3];
idx[dim[k][0]] = i;
idx[dim[k][1]] = j;
for (idx[k] = 0; idx[k] < N[k]; idx[k]++)
ans(idx[0], idx[1], idx[2]) = 0;
}
vec3<int> border[8];
for (int c = 0; c < 8; c++) border[c] = vec3<int>(N[0], N[1], N[2], 0);
vec3<int> cur[8];
for (int c = 0; c < 8; c++) cur[c] = vec3<int>(N[0], N[1], N[2], -1);
vec2<int> B[8][3];
for (int c = 0; c < 8; c++)
for (int k = 0; k < 3; k++) {
int x = (c >> k) & 1;
B[c][k] = vec2<int>(N[dim[k][0]], N[dim[k][1]], 0);
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++) {
int i_r = i, j_r = j;
if ((c >> dim[k][0]) & 1) i_r = N[dim[k][0]] - 1 - i;
if ((c >> dim[k][1]) & 1) j_r = N[dim[k][1]] - 1 - j;
B[c][k](i_r, j_r) = A[k][x](i, j);
}
}
bool stop;
for (int r = 0; r < 2 * min(N[0], min(N[1], N[2])); r++)
for (int c = 0; c < 8; c++) {
if (!c) stop = true;
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
if (i == 0) border[c](i, j, k) |= 1;
if (j == 0) border[c](i, j, k) |= 2;
if (k == 0) border[c](i, j, k) |= 4;
int i_r = i, j_r = j, k_r = k;
if (c & 1) i_r = N[0] - 1 - i;
if (c & 2) j_r = N[1] - 1 - j;
if (c & 4) k_r = N[2] - 1 - k;
if (ans(i_r, j_r, k_r) == 0) cur[c](i, j, k) = 0;
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
int b = border[c](i, j, k);
if (!b) continue;
int idx[3] = {i, j, k};
if (cur[c](i, j, k) == -1)
for (int l = 0; l < 3; l++)
if ((b >> l) & 1) {
int val = B[c][l](idx[dim[l][0]], idx[dim[l][1]]);
if (cur[c](i, j, k) == -1)
cur[c](i, j, k) = val;
else if (cur[c](i, j, k) != val) {
cur[c](i, j, k) = 0;
break;
}
}
if (cur[c](i, j, k) == 0)
for (int l = 0; l < 3; l++) {
idx[l]++;
if (idx[l] != N[l])
border[c](idx[0], idx[1], idx[2]) |= b & (1 << l);
idx[l]--;
}
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
int i_r = i, j_r = j, k_r = k;
if (c & 1) i_r = N[0] - 1 - i;
if (c & 2) j_r = N[1] - 1 - j;
if (c & 4) k_r = N[2] - 1 - k;
int val_cur = cur[c](i, j, k), val_ans = ans(i_r, j_r, k_r);
if (val_cur != -1) {
if (val_ans != -1 && val_cur != val_ans)
ans(i_r, j_r, k_r) = 0, stop = false;
else
ans(i_r, j_r, k_r) = val_cur;
}
}
if (c == 7 && stop) r = 3 * N[0];
}
for (int k = 0; k < 3; k++)
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++)
if (A[k][0](i, j)) {
int idx[3];
idx[dim[k][0]] = i;
idx[dim[k][1]] = j;
idx[k] = 0;
while (idx[k] < N[k] && !ans(idx[0], idx[1], idx[2])) idx[k]++;
if (idx[k] == N[k] || ans(idx[0], idx[1], idx[2]) != A[k][0](i, j)) {
cout << "-1\n";
return 0;
}
idx[k] = N[k] - 1;
while (idx[k] >= 0 && !ans(idx[0], idx[1], idx[2])) idx[k]--;
if (idx[k] == -1 || ans(idx[0], idx[1], idx[2]) != A[k][1](i, j)) {
cout << "-1\n";
return 0;
}
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++)
if (ans(i, j, k) == -1) ans(i, j, k) = 0;
for (int i = 0; i < N[0]; i++) {
if (i) cout << "\n";
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++)
cout << ans(i, j, k) << ((k == N[2] - 1) ? "\n" : " ");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using cat = long long;
template <typename T>
struct vec3 {
int N[3];
vector<T> data;
vec3() = default;
vec3(int N0, int N1, int N2, T val = 0) : N{N0, N1, N2} {
data.resize(N0 * N1 * N2, val);
}
T& operator()(int i0, int i1, int i2) {
return data[(i0 * N[1] + i1) * N[2] + i2];
}
};
template <typename T>
struct vec2 {
int N[2];
vector<T> data;
vec2() = default;
vec2(int N0, int N1, T val = 0) : N{N0, N1} { data.resize(N0 * N1, val); }
T& operator()(int i0, int i1) { return data[i0 * N[1] + i1]; }
};
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
int N[3];
for (int i = 0; i < 3; i++) cin >> N[i];
vec2<int> A[3][2];
int dim[3][2];
for (int k = 0; k < 3; k++)
for (int l = 0; l < 2; l++) {
dim[k][0] = k ? 0 : 1;
dim[k][1] = 3 - k - dim[k][0];
A[k][l] = vec2<int>(N[dim[k][0]], N[dim[k][1]]);
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++) cin >> A[k][l](i, j);
}
vec3<int> ans(N[0], N[1], N[2], -1);
for (int k = 0; k < 3; k++)
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++)
if (!A[k][0](i, j) || !A[k][1](i, j)) {
if (A[k][0](i, j) || A[k][1](i, j)) {
cout << "-1\n";
return 0;
}
int idx[3];
idx[dim[k][0]] = i;
idx[dim[k][1]] = j;
for (idx[k] = 0; idx[k] < N[k]; idx[k]++)
ans(idx[0], idx[1], idx[2]) = 0;
}
vec3<int> border[8];
for (int c = 0; c < 8; c++) border[c] = vec3<int>(N[0], N[1], N[2], 0);
vec3<int> cur[8];
for (int c = 0; c < 8; c++) cur[c] = vec3<int>(N[0], N[1], N[2], -1);
vec2<int> B[8][3];
for (int c = 0; c < 8; c++)
for (int k = 0; k < 3; k++) {
int x = (c >> k) & 1;
B[c][k] = vec2<int>(N[dim[k][0]], N[dim[k][1]], 0);
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++) {
int i_r = i, j_r = j;
if ((c >> dim[k][0]) & 1) i_r = N[dim[k][0]] - 1 - i;
if ((c >> dim[k][1]) & 1) j_r = N[dim[k][1]] - 1 - j;
B[c][k](i_r, j_r) = A[k][x](i, j);
}
}
bool stop;
for (int r = 0; r < min(N[0], min(N[1], N[2])); r++)
for (int c = 0; c < 8; c++) {
if (!c) stop = true;
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
if (i == 0) border[c](i, j, k) |= 1;
if (j == 0) border[c](i, j, k) |= 2;
if (k == 0) border[c](i, j, k) |= 4;
int i_r = i, j_r = j, k_r = k;
if (c & 1) i_r = N[0] - 1 - i;
if (c & 2) j_r = N[1] - 1 - j;
if (c & 4) k_r = N[2] - 1 - k;
if (ans(i_r, j_r, k_r) == 0) cur[c](i, j, k) = 0;
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
int b = border[c](i, j, k);
if (!b) continue;
int idx[3] = {i, j, k};
if (cur[c](i, j, k) == -1)
for (int l = 0; l < 3; l++)
if ((b >> l) & 1) {
int val = B[c][l](idx[dim[l][0]], idx[dim[l][1]]);
if (cur[c](i, j, k) == -1)
cur[c](i, j, k) = val;
else if (cur[c](i, j, k) != val) {
cur[c](i, j, k) = 0;
break;
}
}
if (cur[c](i, j, k) == 0)
for (int l = 0; l < 3; l++) {
idx[l]++;
if (idx[l] != N[l])
border[c](idx[0], idx[1], idx[2]) |= b & (1 << l);
idx[l]--;
}
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
int i_r = i, j_r = j, k_r = k;
if (c & 1) i_r = N[0] - 1 - i;
if (c & 2) j_r = N[1] - 1 - j;
if (c & 4) k_r = N[2] - 1 - k;
int val_cur = cur[c](i, j, k), val_ans = ans(i_r, j_r, k_r);
if (val_cur != -1) {
if (val_ans != -1 && val_cur != val_ans)
ans(i_r, j_r, k_r) = 0, stop = false;
else
ans(i_r, j_r, k_r) = val_cur;
}
}
if (c == 7 && stop) r = N[0];
}
for (int k = 0; k < 3; k++)
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++)
if (A[k][0](i, j)) {
int idx[3];
idx[dim[k][0]] = i;
idx[dim[k][1]] = j;
idx[k] = 0;
while (idx[k] < N[k] && !ans(idx[0], idx[1], idx[2])) idx[k]++;
if (idx[k] == N[k] || ans(idx[0], idx[1], idx[2]) != A[k][0](i, j)) {
cout << "-1\n";
return 0;
}
idx[k] = N[k] - 1;
while (idx[k] >= 0 && !ans(idx[0], idx[1], idx[2])) idx[k]--;
if (idx[k] == -1 || ans(idx[0], idx[1], idx[2]) != A[k][1](i, j)) {
cout << "-1\n";
return 0;
}
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++)
if (ans(i, j, k) == -1) ans(i, j, k) = 0;
for (int i = 0; i < N[0]; i++) {
if (i) cout << "\n";
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++)
cout << ans(i, j, k) << ((k == N[2] - 1) ? "\n" : " ");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using cat = long long;
template <typename T>
struct vec3 {
int N[3];
vector<T> data;
vec3() = default;
vec3(int N0, int N1, int N2, T val = 0) : N{N0, N1, N2} {
data.resize(N0 * N1 * N2, val);
}
T& operator()(int i0, int i1, int i2) {
return data[(i0 * N[1] + i1) * N[2] + i2];
}
};
template <typename T>
struct vec2 {
int N[2];
vector<T> data;
vec2() = default;
vec2(int N0, int N1, T val = 0) : N{N0, N1} { data.resize(N0 * N1, val); }
T& operator()(int i0, int i1) { return data[i0 * N[1] + i1]; }
};
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
int N[3];
for (int i = 0; i < 3; i++) cin >> N[i];
vec2<int> A[3][2];
int dim[3][2];
for (int k = 0; k < 3; k++)
for (int l = 0; l < 2; l++) {
dim[k][0] = k ? 0 : 1;
dim[k][1] = 3 - k - dim[k][0];
A[k][l] = vec2<int>(N[dim[k][0]], N[dim[k][1]]);
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++) cin >> A[k][l](i, j);
}
vec3<int> ans(N[0], N[1], N[2], -1);
for (int k = 0; k < 3; k++)
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++)
if (!A[k][0](i, j) || !A[k][1](i, j)) {
if (A[k][0](i, j) || A[k][1](i, j)) {
cout << "-1\n";
return 0;
}
int idx[3];
idx[dim[k][0]] = i;
idx[dim[k][1]] = j;
for (idx[k] = 0; idx[k] < N[k]; idx[k]++)
ans(idx[0], idx[1], idx[2]) = 0;
}
vec3<int> border[8];
for (int c = 0; c < 8; c++) border[c] = vec3<int>(N[0], N[1], N[2], 0);
vec3<int> cur[8];
for (int c = 0; c < 8; c++) cur[c] = vec3<int>(N[0], N[1], N[2], -1);
vec2<int> B[8][3];
for (int c = 0; c < 8; c++)
for (int k = 0; k < 3; k++) {
int x = (c >> k) & 1;
B[c][k] = vec2<int>(N[dim[k][0]], N[dim[k][1]], 0);
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++) {
int i_r = i, j_r = j;
if ((c >> dim[k][0]) & 1) i_r = N[dim[k][0]] - 1 - i;
if ((c >> dim[k][1]) & 1) j_r = N[dim[k][1]] - 1 - j;
B[c][k](i_r, j_r) = A[k][x](i, j);
}
}
for (int r = 0; r < min(N[0], min(N[1], N[2])); r++)
for (int c = 0; c < 8; c++) {
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
if (i == 0) border[c](i, j, k) |= 1;
if (j == 0) border[c](i, j, k) |= 2;
if (k == 0) border[c](i, j, k) |= 4;
int i_r = i, j_r = j, k_r = k;
if (c & 1) i_r = N[0] - 1 - i;
if (c & 2) j_r = N[1] - 1 - j;
if (c & 4) k_r = N[2] - 1 - k;
if (ans(i, j, k) == 0) cur[c](i_r, j_r, k_r) = 0;
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
if (!border[c](i, j, k)) continue;
int idx[3] = {i, j, k};
if (cur[c](i, j, k) == -1)
for (int l = 0; l < 3; l++)
if ((border[c](i, j, k) >> l) & 1) {
if (cur[c](i, j, k) == -1)
cur[c](i, j, k) = B[c][l](idx[dim[l][0]], idx[dim[l][1]]);
else if (cur[c](i, j, k) !=
B[c][l](idx[dim[l][0]], idx[dim[l][1]]))
cur[c](i, j, k) = 0;
}
int b = border[c](i, j, k);
if (cur[c](i, j, k) == 0) {
for (int l = 0; l < 3; l++) {
idx[l]++;
if (idx[l] != N[l])
border[c](idx[0], idx[1], idx[2]) |= b & (1 << l);
idx[l]--;
}
} else {
}
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
int i_r = i, j_r = j, k_r = k;
if (c & 1) i_r = N[0] - 1 - i;
if (c & 2) j_r = N[1] - 1 - j;
if (c & 4) k_r = N[2] - 1 - k;
if (cur[c](i, j, k) != -1) {
if (ans(i_r, j_r, k_r) != -1 &&
cur[c](i, j, k) != ans(i_r, j_r, k_r))
ans(i_r, j_r, k_r) = 0;
else
ans(i_r, j_r, k_r) = cur[c](i, j, k);
}
}
}
for (int k = 0; k < 3; k++)
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++)
if (A[k][0](i, j)) {
int idx[3];
idx[dim[k][0]] = i;
idx[dim[k][1]] = j;
idx[k] = 0;
while (idx[k] < N[k] && !ans(idx[0], idx[1], idx[2])) idx[k]++;
if (idx[k] == N[k] || ans(idx[0], idx[1], idx[2]) != A[k][0](i, j)) {
cout << "-1\n";
return 0;
}
idx[k] = N[k] - 1;
while (idx[k] >= 0 && !ans(idx[0], idx[1], idx[2])) idx[k]--;
if (idx[k] == -1 || ans(idx[0], idx[1], idx[2]) != A[k][1](i, j)) {
cout << "-1\n";
return 0;
}
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++)
if (ans(i, j, k) == -1) ans(i, j, k) = 0;
for (int i = 0; i < N[0]; i++) {
if (i) cout << "\n";
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++)
cout << ans(i, j, k) << ((k == N[2] - 1) ? "\n" : " ");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T1, typename T2>
inline void chkmin(T1 &x, const T2 &y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1 &x, const T2 &y) {
if (x < y) x = y;
}
struct triple {
int x, y, z;
triple() {}
triple(int _x, int _y, int _z) { x = _x, y = _y, z = _z; }
};
int n, m, k;
vector<vector<int>> up, down, left228, right228, top, bottom;
void Read(vector<vector<int>> &have, int a, int b) {
have.resize(a, vector<int>(b));
for (auto &i : have)
for (auto &j : i) cin >> j;
}
void read() {
cin >> n >> m >> k;
Read(left228, m, k);
Read(right228, m, k);
Read(top, n, k);
Read(bottom, n, k);
Read(down, n, m);
Read(up, n, m);
}
bool operator==(const triple &a, const triple &b) {
return tie(a.x, a.y, a.z) == tie(b.x, b.y, b.z);
}
vector<vector<vector<triple>>> par[3];
vector<vector<vector<int>>> Min[3], Max[3];
triple get_par(triple a, int it) {
if (a == par[it][a.x][a.y][a.z]) return a;
return par[it][a.x][a.y][a.z] = get_par(par[it][a.x][a.y][a.z], it);
}
void uni(triple a, triple b, int it) {
a = get_par(a, it);
b = get_par(b, it);
if (a == b) return;
par[it][b.x][b.y][b.z] = a;
chkmin(Min[it][a.x][a.y][a.z], Min[it][b.x][b.y][b.z]);
chkmax(Max[it][a.x][a.y][a.z], Max[it][b.x][b.y][b.z]);
}
void no() {
cout << -1 << endl;
exit(0);
}
vector<vector<vector<bool>>> used;
queue<triple> q;
int get_color(int x, int y, int z) {
set<int> color;
triple a;
if (x != 0) {
a = {x - 1, y, z};
a = get_par(a, 0);
}
if (x == 0 || (Min[0][a.x][a.y][a.z] == 0 && used[x - 1][y][z])) {
color.insert(left228[y][z]);
}
if (x != n - 1) {
a = {x + 1, y, z};
a = get_par(a, 0);
}
if (x == n - 1 || (Max[0][a.x][a.y][a.z] == n - 1 && used[x + 1][y][z])) {
color.insert(right228[y][z]);
}
if (y != 0) {
a = {x, y - 1, z};
a = get_par(a, 1);
}
if (y == 0 || (Min[1][a.x][a.y][a.z] == 0 && used[x][y - 1][z])) {
color.insert(top[x][z]);
}
if (y != m - 1) {
a = {x, y + 1, z};
a = get_par(a, 1);
}
if (y == m - 1 || (Max[1][a.x][a.y][a.z] == m - 1 && used[x][y + 1][z])) {
color.insert(bottom[x][z]);
}
if (z != 0) {
a = {x, y, z - 1};
a = get_par(a, 2);
}
if (z == 0 || (Min[2][a.x][a.y][a.z] == 0 && used[x][y][z - 1])) {
color.insert(down[x][y]);
}
if (z != k - 1) {
a = {x, y, z + 1};
a = get_par(a, 2);
}
if (z == k - 1 || (Max[2][a.x][a.y][a.z] == k - 1 && used[x][y][z + 1])) {
color.insert(up[x][y]);
}
if (color.empty()) return -1;
if (color.size() >= 2) return 0;
return *color.begin();
}
void build() {
used.assign(n, vector<vector<bool>>(m, vector<bool>(k, false)));
for (int i = 0; i < 3; i++) {
par[i].resize(n, vector<vector<triple>>(m, vector<triple>(k)));
Min[i].resize(n, vector<vector<int>>(m, vector<int>(k)));
Max[i].resize(n, vector<vector<int>>(m, vector<int>(k)));
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
for (int it = 0; it < 3; it++) {
par[it][x][y][z] = {x, y, z};
}
Min[0][x][y][z] = x;
Max[0][x][y][z] = x;
Min[1][x][y][z] = y;
Max[1][x][y][z] = y;
Min[2][x][y][z] = z;
Max[2][x][y][z] = z;
}
}
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
if (!get_color(x, y, z)) {
q.push({x, y, z});
used[x][y][z] = true;
}
}
}
}
}
vector<int> dx = {-1, 1, 0, 0, 0, 0};
vector<int> dy = {0, 0, -1, 1, 0, 0};
vector<int> dz = {0, 0, 0, 0, -1, 1};
bool check(int x, int y, int z) {
return x >= 0 && x < n && y >= 0 && y < m && z >= 0 && z < k;
}
void del(int x, int y, int z) {
for (int i = 0; i < 6; i++) {
int nx = x + dx[i], ny = y + dy[i], nz = z + dz[i];
if (!check(nx, ny, nz)) continue;
if (!used[nx][ny][nz]) continue;
uni({x, y, z}, {nx, ny, nz}, i / 2);
}
}
void bfs() {
while (!q.empty()) {
auto v = q.front();
int x = v.x, y = v.y, z = v.z;
q.pop();
del(x, y, z);
for (int i = 0; i < 6; i++) {
int nx = x + dx[i], ny = y + dy[i], nz = z + dz[i];
if (!check(nx, ny, nz)) continue;
if (get_color(nx, ny, nz)) continue;
if (used[nx][ny][nz]) {
auto a = triple(nx, ny, nz);
a = get_par(a, i / 2);
if (i / 2 == 0) {
if (i % 2 == 0) {
nx = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
nx = Max[i / 2][a.x][a.y][a.z] + 1;
}
} else if (i / 2 == 1) {
if (i % 2 == 0) {
ny = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
ny = Max[i / 2][a.x][a.y][a.z] + 1;
}
} else {
if (i % 2 == 0) {
nz = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
nz = Max[i / 2][a.x][a.y][a.z] + 1;
}
}
if (!check(nx, ny, nz)) continue;
if (get_color(nx, ny, nz)) continue;
}
if (used[nx][ny][nz]) continue;
used[nx][ny][nz] = true;
q.push({nx, ny, nz});
}
}
}
void check_ans() {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
bool flag = false;
for (int x = 0; x < n; x++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (left228[y][z] || right228[y][z]) {
no();
}
}
}
}
for (int x = 0; x < n; x++) {
for (int z = 0; z < k; z++) {
bool flag = false;
for (int y = 0; y < m; y++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (top[x][z] || bottom[x][z]) {
no();
}
}
}
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
bool flag = false;
for (int z = 0; z < k; z++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (up[x][y] || down[x][y]) {
no();
}
}
}
}
}
void run() {
build();
bfs();
check_ans();
}
void write() {
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
int ans = get_color(x, y, z);
if (ans == -1) ans = 0;
if (used[x][y][z]) ans = 0;
cout << ans << " ";
}
}
}
cout << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
read();
run();
write();
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.