A. Not Shading
#ifndef DEBUG
#pragma GCC optimize("O2")
#endif
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<limits.h>
#include<assert.h>
using namespace std;
#define all(X) (X).begin(), (X).end()
#define rep(I,A,B) for(int I=(A);I<(B);I++)
#define repd(I,A,B) for(int I=(A);I>(B);I--)
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;
typedef vector<PII> VPII;
typedef vector<LL> VLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &&head, U &&... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W("{"); _W(x.first); putchar(','); _W(x.second); _W("}");}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() { }
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
int main()
{
#ifdef DEBUG
freopen("1.in","r",stdin);
//freopen("tmp.out","w",stdout);
#endif
int tc;
R(tc);
rep(_,0,tc) {
int n,m,r,c;
R(n,m,r,c);
char chr;
int cnt=0; // 总的黑方块数量
int cnt1=0; // 第r行第c列黑方块数量
int cnt2=0; // (r,c)处是否是黑方块
rep(i,1,n+1)
rep(j,1,m+1) {
R(chr);
if(chr=='B') {
cnt++;
if(i==r || j==c)
cnt1++;
if(i==r && j==c)
cnt2++;
}
}
if(cnt2==1) {
W(0);
}
else {
if(cnt1>0)
W(1);
else {
if(cnt>0)
W(2);
else
W(-1);
}
}
}
return 0;
}
B. Not Sitting
#ifndef DEBUG
#pragma GCC optimize("O2")
#endif
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<limits.h>
#include<assert.h>
using namespace std;
#define all(X) (X).begin(), (X).end()
#define rep(I,A,B) for(int I=(A);I<(B);I++)
#define repd(I,A,B) for(int I=(A);I>(B);I--)
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;
typedef vector<PII> VPII;
typedef vector<LL> VLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &&head, U &&... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W("{"); _W(x.first); putchar(','); _W(x.second); _W("}");}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() { }
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
int main()
{
#ifdef DEBUG
freopen("b-1.in","r",stdin);
//freopen("tmp.out","w",stdout);
#endif
int tc;
R(tc);
rep(_,0,tc) {
//W("tc",_);
int n,m;
R(n,m);
VI a;
int x1=ceil(n/2.0),y1=ceil(m/2.0); // Rahul position
rep(i,1,n+1) {
rep(j,1,m+1) {
int d1=abs(1-i)+abs(1-j);
int d2=abs(n-i)+abs(1-j);
int d3=abs(1-i)+abs(m-j);
int d4=abs(n-i)+abs(m-j);
int ans = max({d1,d2,d3,d4});
a.push_back(ans);
}
}
sort(all(a));
W(VI(a.begin(),a.begin()+n*m));
}
return 0;
}
C. Not Assigning
构造一棵素数树。将其视为链,对每条边交替赋值2、3即可。
#ifndef DEBUG
#pragma GCC optimize("O2")
#endif
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<limits.h>
#include<assert.h>
using namespace std;
#define all(X) (X).begin(), (X).end()
#define rep(I,A,B) for(int I=(A);I<(B);I++)
#define repd(I,A,B) for(int I=(A);I>(B);I--)
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;
typedef vector<PII> VPII;
typedef vector<LL> VLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &&head, U &&... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W("{"); _W(x.first); putchar(','); _W(x.second); _W("}");}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() { }
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
void dfs(vector<VPII> &g, VI &vis, int u, int last, VI &ans) {
vis[u]=1;
rep(i,0,g[u].size()) {
int v = g[u][i].first;
if(vis[v]) continue;
int idx = g[u][i].second;
if(last==2) {
ans[idx] = 3;
last = 3;
}
else {
ans[idx] = 2;
last = 2;
}
dfs(g,vis,v,last,ans);
}
}
void solve(int n) {
VI d(n+1);
vector<VPII> g(n+1);
rep(i,0,n-1) {
int u,v;
R(u,v);
g[u].push_back({v,i});
g[v].push_back({u,i});
d[u]++;
d[v]++;
}
rep(i,1,n+1) {
if(d[i]>2) {
W(-1);
return;
}
}
VI vis(n+1), ans(n-1);
rep(i,1,n+1) {
if(d[i]==1) {
dfs(g,vis,i,3,ans);
break;
}
}
W(ans);
}
int main()
{
#ifdef DEBUG
freopen("c-1.in","r",stdin);
//freopen("tmp.out","w",stdout);
#endif
int tc;
R(tc);
rep(_,0,tc) {
//W("tc",_);
int n;
R(n);
solve(n);
}
return 0;
}