targan+缩点
class targan {
private:
int n, m, cnt, cntb;
vector<vector<int>> edg, nedg, belong;
vector<int> dfn, low, mmp;
vector<bool> instack;
stack<int> s;
vector<vector<int>> fa;
public:
targan(int n) : cnt(0), cntb(0) {
this->n = n;
init();
}
void init() {
edg.resize(n + 1);
belong.resize(n + 1);
dfn.resize(n + 1);
low.resize(n + 1);
instack.resize(n + 1);
}
void addedg(int m) {
this->m = m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
if (a[u] > a[v]) swap(u, v);
edg[u].push_back(v);
if (a[u] * 2 >= a[v])
edg[v].push_back(u);
}
}
void gan() {
for (int i = 1; i <= n; i++)
if (!dfn[i]) Tarjan(i);
}
void suodian() {
nedg.resize(cntb + 2);
mmp.resize(n + 2);
for (int i = 1; i <= cntb; i++) {
for (int j = 0; j < belong[i].size(); ++j) {
mmp[belong[i][j]] = i;
}
}
for (int i = 1; i <= n; i++) {
for (auto j : edg[i]) {
if (mmp[i] != mmp[j]) nedg[mmp[i]].push_back(mmp[j]);
}
}
}
int dfs() {
fa.resize(cntb + 2);
int ans = 0;
if1(cntb) {
for (auto j : nedg[i]) {
fa[j].push_back(i);
if (fa[j].size() > 1) return 0;
}
if (fa[i].empty()) ans = i;
}
return belong[ans].size();
}
void outtj() {
for (int i = 1; i <= cntb; ++i) {
cout<<"强连通分量第 "<<i<<" 个: ";
for (int j = 0; j < belong[i].size(); ++j) {
cout << belong[i][j] << " ";
}
cout << endl;
}
}
void Tarjan(int u) {
++cnt;
dfn[u] = low[u] = cnt;
s.push(u);
instack[u] = true;
for (int i = 0; i < edg[u].size(); ++i) {
int v = edg[u][i];
if (!dfn[v]) {
Tarjan(v);
low[u] = min(low[u], low[v]);
} else if (instack[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u]) {
++cntb;
int node;
do {
node = s.top();
s.pop();
instack[node] = false;
belong[cntb].push_back(node);
} while (node != u);
}
}
};
void solve() {
int n;
cin >> n;
if1(n) cin >> a[i];
targan tj(n);
tj.addedg(n - 1);
tj.gan();
tj.suodian();
int t = tj.dfs();
if (!t) cout << 0 << endl;
else cout << t << endl;
}