T1:
C++#include <iostream>
using namespace std;
const int maxn = 1e5 + 5;
int n,q,op,x,y,f[maxn << 1],g[maxn << 1],sz[maxn << 1];
int find(int x){
if (x == f[x]) return x;
return f[x] = find(f[x]);
}
void merge(int x , int y){
x = find(x) , y = find(y);
if (x == y) return;
if (g[x] > g[y]) swap(x , y);
g[y] += g[x];
g[x] = 0;
sz[y] += sz[x];
sz[x] = 0;
f[x] = y;
}
int main(){
freopen("team.in","r",stdin);
freopen("team.out","w",stdout);
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> q;
for (int i = 1;i <= n;i++) sz[i] = 1;
for (int i = 1;i <= (n << 1);i++) f[i] = i,g[i] = 1;
while (q--){
cin >> op >> x;
if (op == 2) cout << sz[find(x)] << '\n';
else{
cin >> y;
if (op == 0){
if (find(x) == find(y + n) || find(x + n) == find(y)) cout << "false\n";
else{
merge(x , y);
merge(x + n , y + n);
cout << "true\n";
}
}
else{
if (find(x) == find(y) || find(x + n) == find(y + n)) cout << "false\n";
else{
merge(x , y + n);
merge(x + n , y);
cout << "true\n";
}
}
}
}
return 0;
}
T2:
C++#include <iostream>
#include <vector>
using namespace std;
const int maxn = 2e5 + 5;
struct node{
int v;
pair <int , int> u , d , l , r;
}tmp;
vector <node> a[maxn];
int n,m,q;
vector <bool> b[maxn];
void del(pair <int , int> y){
node x = a[y.first][y.second];
a[x.l.first][x.l.second].r = x.r;
a[x.r.first][x.r.second].l = x.l;
a[x.u.first][x.u.second].d = x.d;
a[x.d.first][x.d.second].u = x.u;
b[y.first][y.second] = 1;
}
int main(){
freopen("eat.in","r",stdin);
freopen("eat.out","w",stdout);
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m >> q;
for (int i = 0;i <= m + 1;i++) a[0].push_back(tmp) , a[n + 1].push_back(tmp);
for (int i = 1;i <= n;i++) a[i].push_back(tmp);
for (int i = 1;i <= n;i++){
for (int j = 1;j <= m;j++){
int x;
cin >> x;
a[i].push_back({x , make_pair(i - 1 , j) , make_pair(i + 1 , j) , make_pair(i , j - 1) , make_pair(i , j + 1)});
}
}
for (int i = 1;i <= n;i++) a[i].push_back(tmp);
for (int i = 1;i <= n;i++){
for (int j = 0;j <= m;j++) b[i].push_back(0);
}
int x,y;
char op;
for (int i = 1;i <= q;i++){
cin >> x >> y >> op;
if (b[x][y]){
cout << "impossible\n";
continue;
}
pair <int , int> now;
if (op == 'u') now = a[x][y].u;
if (op == 'l') now = a[x][y].l;
if (op == 'd') now = a[x][y].d;
if (op == 'r') now = a[x][y].r;
if (now.first < 1 || now.first > n || now.second < 1 || now.second > m){
cout << "impossible\n";
continue;
}
if (a[now.first][now.second].v >= a[x][y].v){
cout << "lose\n";
continue;
}
cout << "win\n";
del(now);
}
return 0;
}
T3:
C++#include <iostream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 5e4 + 5 , mod = 1e9 + 7;
vector <int> e[maxn],g[maxn];
long long r[maxn],o[maxn],a[maxn],b[maxn],n,m;
void topsort1(){
queue <int> q;
for (int i = 1;i <= n;i++){
if (r[i] == 0) q.push(i),a[i] = 1;
}
while (!q.empty()){
int x = q.front();
q.pop();
for (int y : e[x]){
a[y] += a[x];
a[y] %= mod;
r[y]--;
if (r[y] == 0) q.push(y);
}
}
}
void topsort2(){
queue <int> q;
for (int i = 1;i <= n;i++){
if (o[i] == 0) q.push(i),b[i] = 1;
}
while (!q.empty()){
int x = q.front();
q.pop();
for (int y : g[x]){
b[y] += b[x];
b[y] %= mod;
o[y]--;
if (o[y] == 0) q.push(y);
}
}
}
int main(){
freopen("food.in","r",stdin);
freopen("food.out","w",stdout);
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
int x,y;
while (m--){
cin >> x >> y;
e[x].push_back(y);
g[y].push_back(x);
o[x]++;
r[y]++;
}
topsort1();
long long ans = 0,temp = 0;
for (int i = 1;i <= n;i++){
if (o[i] == 0){
ans += a[i];
ans %= mod;
}
}
topsort2();
for (int i = 1;i <= n;i++){
temp = ans;
for (int y : e[i]){
temp = ((temp - a[i] * b[y]) % mod + mod) % mod;
}
cout << temp << '\n';
}
return 0;
}
T4:
C++#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxl = 50;
struct node{
int l;
int a[maxl];
void print(){
for (int i = l;i >= 1;i--){
cout << a[i];
}
cout << endl;
}
};
int n;
node ans,x1,x2,k;
string x;
void init(node& x){
x.l = 0;
memset(x.a , 0 , sizeof x.a);
}
node to(string s){
node ret;
init(ret);
ret.l = s.size();
for (int i = 1;i <= ret.l;i++){
ret.a[i] = s[ret.l - i] - '0';
}
return ret;
}
bool operator < (const node &x , const node &y){
if (x.l != y.l) return x.l < y.l;
for (int i = x.l;i >= 1;i--){
if (x.a[i] != y.a[i]) return x.a[i] < y.a[i];
}
return 0;
}
bool operator > (const node &x , const node &y){
if (x.l != y.l) return x.l > y.l;
for (int i = x.l;i >= 1;i--){
if (x.a[i] != y.a[i]) return x.a[i] > y.a[i];
}
return 0;
}
node operator + (const node& x , const node& y){
node z;
init(z);
z.l = max(x.l , y.l);
for (int i = 1;i <= z.l;i++){
z.a[i] += x.a[i] + y.a[i];
z.a[i + 1] = z.a[i] / 10;
z.a[i] %= 10;
}
if (z.a[z.l + 1]) z.l++;
return z;
}
node operator - (const node& x , const node& y){
node z;
init(z);
z.l = x.l;
for (int i = 1;i <= z.l;i++){
z.a[i] += x.a[i] - y.a[i];
if (z.a[i] >= 0){
z.a[i + 1] = z.a[i] / 10;
z.a[i] %= 10;
}
else{
z.a[i + 1] = -1;
z.a[i] = z.a[i] + 10;
}
}
while (z.a[z.l] == 0 && z.l > 1) z.l--;
return z;
}
node operator * (const node& x , const node& y){
node z;
init(z);
z.l = x.l + y.l - 1;
for (int i = 1;i <= x.l;i++){
for (int j = 1;j <= y.l;j++){
z.a[i + j - 1] += x.a[i] * y.a[j];
z.a[i + j] += z.a[i + j - 1] / 10;
z.a[i + j - 1] %= 10;
}
}
if (z.a[z.l + 1] != 0) z.l++;
while(z.a[z.l] == 0&&z.l > 1) z.l--;
return z;
}
int main(){
freopen("merge.in","r",stdin);
freopen("merge.out","w",stdout);
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> x;
if (x[0] == '-') k = to(x.substr(1));
else k = to(x);
if (x[0] != '-'){
priority_queue <node> q;
for (int i = 1;i <= n;i++){
cin >> x;
q.push(to(x));
}
for (int i = 1;i < n;i++){
init(x1);
init(x2);
x1 = q.top();
q.pop();
x2 = q.top();
q.pop();
ans = ans + (x1 - k) * (x2 - k);
q.push(x1 + x2);
}
ans.print();
}
else{
priority_queue <node , vector <node> , greater<node> > q;
for (int i = 1;i <= n;i++){
cin >> x;
q.push(to(x));
}
for (int i = 1;i < n;i++){
init(x1);
init(x2);
x1 = q.top();
q.pop();
x2 = q.top();
q.pop();
ans = ans + (x1 + k) * (x2 + k);
q.push(x1 + x2);
}
ans.print();
}
return 0;
}