如果你只过了前五个点,WA 45pts,检查你的 dfs 有没有写挂。
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N = 1e3 + 5;
template<class Tp>
class Ufs {
private:
map<Tp, Tp> f;
Tp root(Tp a) {
if(f[a] == a) return a;
f[a] = root(f[a]);
return f[a];
}
Tp a[N];
public:
map<Tp, int> s;
void init(Tp *b, Tp *e) {
int z = 0;
for(Tp *i = b; i < e; i++) f[*i] = *i, s[*i] = 1;
}
void init(int n) {
for(Tp i = 1; i <= n; i++) a[i] = i;
init(a + 1, a + n + 1);
}
bool same(Tp a, Tp b) {
Tp p = root(a), q = root(b);
return p == q;
}
void merge(Tp a, Tp b) {
Tp p = root(a), q = root(b);
if(p == q) return;
if(s[p] > s[q]) swap(p, q);
f[p] = q;
s[q] += s[p];
}
}; Ufs<int> g;
int n, m, x[N], y[N]; set<char> c; vector<char> d;
vector<pair<int, int>> s;
int randint(int l, int r) {
return rand() % (r - l + 1) + l;
}
void work(char a, char b) {
int l = randint(0, 3);
cout<<a; while(l--) cout<<char(randint(0, 25) + 'a'); cout<<b<<endl;
}
void solve() {
// cout<<"solve"<<endl;
n = 0, m = randint(1, 5);
c.clear(), s.clear(), d.clear();
while(c.size() < m) c.insert(randint(0, 25) + 'a');
for(char i : c) d.push_back(i); // cout<<i; cout<<endl;
int t = randint(0, 1);
// cout<<"qwq "<<n<<" "<<m<<" "<<t<<endl;
if(t || m == 1) {
for(int i = 1; i <= m; i++) x[i] = y[i] = randint(1, 3), n += x[i] + y[i];
} else {
int f = randint(1, m), e = randint(1, m);
while(e == f) e = randint(1, m);
for(int i = 1; i <= m; i++) {
if(i == f) x[i] = (y[i] = randint(1, 3) + 1);
else if(i == e) x[i] = (y[i] = randint(1, 3) - 1);
else x[i] = y[i] = randint(1, 3);
n += x[i] + y[i];
}
}
for(int i = 1; i <= n / 2; i++) {
int u = randint(1, m), v = randint(1, m);
while(!x[u] || !y[v]) u = randint(1, m), v = randint(1, m);
x[u]--, y[v]--, s.push_back({u, v});
}
// cout<<"qwq"<<endl;
g.init(m);
for(auto [u, v] : s) g.merge(u, v);
int b = 1;
for(int i = 1; i <= m; i++) if(g.s[i] == m) b = 0;
if(b) {
// cout<<"qwq"<<endl;
solve();
return;
}
cout<<n / 2<<endl;
for(auto [u, v] : s) {
// cout<<u<<" "<<v<<endl;
work(d[u - 1], d[v - 1]);
}
}
signed main() {
srand(time(0));
solve();
}