UVA-Proving Equivalences La 4287
题目很好懂, 讲下思路;
把每个命题看成节点, 推导视为有向边, 得到一个有向图G, 那么题意就是如何添加边, 使G强连通(每个点都能互相到达), 把G中的强连通分量找出后, 缩成一个点使G变成一个DAG, 接下来设这个DAG上有a个节点出度使0, b个节点入度为0, max(a, b)
这就没什么好证明的了, 很显然是这样的。。。
code
#include <stack> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N 50005 #define next Next #define begin Begin #define mem(a) memset(a, 0, sizeof(a)) #define rep(i, j, k) for(int i=j; i<=k; ++i) #define erep(i, u) for(int i=begin[u]; i; i=next[i]) void read(int &x){ char c=getchar();x=0;while(c<'0' || c>'9')c=getchar(); while(c>='0' && c<='9')x=x*10+c-'0', c=getchar(); } int begin[N<<1], to[N<<1], next[N<<1], e; void add(int x, int y){ to[++e]=y; next[e]=begin[x]; begin[x]=e; } int n, m, low[N], pre[N], sccid[N], scc_cnt, clock_; stack<int>s; void dfs(int u){ pre[u]=low[u]=++clock_; s.push(u); erep(i, u){ int v=to[i]; if(!pre[v])dfs(v),low[u]=min(low[u], low[v]); else if(!sccid[v])low[u]=min(low[u], pre[v]); //notice! array low can only reach the point which is in the same SCC; } if(pre[u] == low[u]){ scc_cnt++; while(1){ int x=s.top();s.pop(); sccid[x]=scc_cnt;if(x == u)break; } } } void solve(){ clock_ = scc_cnt = 0; mem(pre);mem(sccid); rep(i, 1, n) if(!pre[i]) dfs(i); } int res1[N], res2[N];//no point can reach it, it can't reach any other point; int main(){ #ifndef ONLINE_JUDGE freopen("data.in", "r", stdin); freopen("result.out", "w", stdout); #endif int _; read(_); while(_--){ e=0;mem(begin); read(n);read(m); rep(i, 1, m){ int u, v; read(u);read(v); add(u, v); } solve(); rep(i, 1, scc_cnt)res1[i] = res2[i] = 1; rep(u, 1, n) erep(i, u){ int v=to[i]; if(sccid[v] != sccid[u])res1[sccid[v]] = res2[sccid[u]] = 0; } int a, b;a = b = 0; rep(i, 1, scc_cnt){ if(res1[i]) a++; if(res2[i]) b++; } printf("%d\n", scc_cnt==1? 0: max(a, b)); } return 0; }