HDOJ 4888 Redraw Beautiful Drawings && HDOJ 4975 A simple Gaussian elimination problem

解题思路:

这两道题题目大致相同,都是已知一个矩阵每一行的和和每一列的和,并且每个点的数小于K  还原原矩阵并判断答案是否唯一。建图方式相同,新建一个原点S 和一个汇点T ,S到行连边,容量为该行之和,列到T连边,容量为该列之和, 对于每一个点 i 和 j ,i 行向 j 列连边 , 容量为K , 求一遍最大流。并且通过判断是否存在环来判断是否唯一。

区别在于 第二道题N 与 M 均扩大,找环需要采用更高效的算法,这里采用Tarjan 算法来找环。

HDOJ 4888

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#define LL long long
using namespace std;
const int maxn = 10000 + 10;
const int INF = 100000000;
struct Edge
{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int w):from(u),to(v),cap(c),flow(w){ }
};
int n , s , t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init()
{
    for(int i=0;i<=n+1;i++)
        G[i].clear();
    edges.clear();
}
void AddEdge(int from,int to,int cap)
{
    edges.push_back(Edge(from,to,cap,0));
    edges.push_back(Edge(to,from,0,0));
    int M = edges.size();
    G[from].push_back(M-2);
    G[to].push_back(M-1);
}
bool BFS()
{
    memset(vis,0,sizeof(vis));
    queue<int>Q;
    Q.push(s);
    d[s] = 0;
    vis[s] = 1;
    while(!Q.empty())
    {
        int x = Q.front();Q.pop();
        for(int i=0;i<G[x].size();i++)
        {
            Edge& e = edges[G[x][i]];
            if(!vis[e.to] && e.cap > e.flow)
            {
                vis[e.to] = 1;
                d[e.to] = d[x] + 1;
                Q.push(e.to);
            }
        }
    }
    return vis[t];
}
int DFS(int x,int a)
{
    if(x == t || a == 0) return a;
    int flow = 0 , f;
    for(int &i = cur[x];i<G[x].size();i++)
    {
        Edge& e = edges[G[x][i]];
        if(d[x] + 1 == d[e.to] && (f = DFS(e.to,min(a,e.cap-e.flow))) > 0)
        {
            e.flow += f;
            edges[G[x][i]^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}
int Maxflow()
{
    int flow = 0;
    while(BFS())
    {
        memset(cur,0,sizeof(cur));
        flow += DFS(s,INF);
    }
    return flow;
}
int pre[maxn] , lowlink[maxn] ,sccno[maxn] , fa[maxn], dfs_clock , scc_cnt , flag;
stack<int> S;
void dfs(int u)
{
    pre[u] = lowlink[u] = ++dfs_clock;
    S.push(u);
    for(int i=0;i<G[u].size();i++)
    {
        Edge& e = edges[G[u][i]];
        if(e.cap == e.flow) continue;
        int v = e.to;
        if(!pre[v])
        {
            fa[v] = u;
            dfs(v);
            if(!flag) return ;
            lowlink[u] = min(lowlink[u] , lowlink[v]);
        }
        else if(!sccno[v])
        {
         lowlink[u] = min(lowlink[u] , pre[v]);
         if(v != fa[u]) {flag = 0 ; return ;}
        }
    }
    if(lowlink[u] == pre[u])
    {
        scc_cnt++;
        for(;;)
        {
            int x = S.top(); S.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}
bool find_scc()
{
    dfs_clock = scc_cnt = 0; flag = 1;
    memset(pre , 0, sizeof(pre));
    memset(lowlink , 0, sizeof(lowlink));
    memset(sccno , 0 , sizeof(sccno));
    for(int i=0;i<n;i++)
    if(!pre[i]) { dfs(i); if(!flag) return 0;}
    return 1;
}
int main()
{
    int T;
    int N , M , K;
    while(scanf("%d%d%d",&N,&M,&K)!=EOF)
    {
		s = 0 , t = N + M + 1;
        n = t + 1; init();
        int sumr = 0 , sumc = 0;
        int x;
        for(int i=1;i<=N;i++)
        {
            scanf("%d",&x);
            AddEdge(s , i , x);
            sumr += x;
        }
        for(int i=1;i<=M;i++)
        {
            scanf("%d",&x);
            AddEdge(i + N , t , x);
            sumc += x;
        }
		if (sumr != sumc){ printf("Impossible\n");continue; }
        for(int i=1;i<=N;i++)
        {
            for(int j=1;j<=M;j++)
            {
                AddEdge(i , j + N , K);
            }
        }
        int ans = Maxflow();
        if(ans != sumr) { printf("Impossible\n"); continue; }
        if(find_scc())
		{
			printf("Unique\n");
			for(int i=1;i<=N;i++)
			{
				for(int j=1;j<=M;j++)
				{
					Edge& e = edges[G[i][j]];
				    cout<<e.flow;
				    if(j < M) cout<<' ';
				}
				cout<<endl;
			}
		}
        else printf("Not Unique\n");
    }
    return 0;
}

HDOJ 4975 
<span style="color:#0000ff;">#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#define LL long long
</span><strong><span style="color:#0000ff;">using namespace</span></strong> std<strong><span style="color:#ff00ff;">;</span><span style="color:#0000ff;">
const</span><span style="color:blue;"> int</span></strong> maxn<strong><span style="color:#ff00ff;"> =</span></strong><span style="color:#cc3300;"> 10000</span><strong><span style="color:#ff00ff;"> +</span></strong><span style="color:#cc3300;"> 10</span><strong><span style="color:#ff00ff;">;</span><span style="color:#0000ff;">
const</span><span style="color:blue;"> int</span></strong> INF<strong><span style="color:#ff00ff;"> =</span></strong><span style="color:#cc3300;"> 100000000</span><strong><span style="color:#ff00ff;">;</span><span style="color:#0000ff;">
struct</span></strong> Edge<strong><span style="color:#ff00ff;">
{</span><span style="color:blue;">
    int</span></strong> from<strong><span style="color:#ff00ff;">,</span></strong>to<strong><span style="color:#ff00ff;">,</span></strong>cap<strong><span style="color:#ff00ff;">,</span></strong>flow<strong><span style="color:#ff00ff;">;</span></strong>
    Edge<strong><span style="color:#ff00ff;">(</span><span style="color:blue;">int</span></strong> u<strong><span style="color:#ff00ff;">,</span><span style="color:blue;">int</span></strong> v<strong><span style="color:#ff00ff;">,</span><span style="color:blue;">int</span></strong> c<strong><span style="color:#ff00ff;">,</span><span style="color:blue;">int</span></strong> w<strong><span style="color:#ff00ff;">):</span></strong>from<strong><span style="color:#ff00ff;">(</span></strong>u<strong><span style="color:#ff00ff;">),</span></strong>to<strong><span style="color:#ff00ff;">(</span></strong>v<strong><span style="color:#ff00ff;">),</span></strong>cap<strong><span style="color:#ff00ff;">(</span></strong>c<strong><span style="color:#ff00ff;">),</span></strong>flow<strong><span style="color:#ff00ff;">(</span></strong>w<strong><span style="color:#ff00ff;">){ }
};</span><span style="color:blue;">
int</span></strong> n<strong><span style="color:#ff00ff;"> ,</span></strong> s<strong><span style="color:#ff00ff;"> ,</span></strong> t<strong><span style="color:#ff00ff;">;</span></strong>
vector<strong><span style="color:#ff00ff;"><</span></strong>Edge<strong><span style="color:#ff00ff;">></span></strong> edges<strong><span style="color:#ff00ff;">;</span></strong>
vector<strong><span style="color:#ff00ff;"><</span><span style="color:blue;">int</span><span style="color:#ff00ff;">></span></strong> G<strong><span style="color:#ff00ff;">[</span></strong>maxn<strong><span style="color:#ff00ff;">];</span><span style="color:blue;">
bool</span></strong> vis<strong><span style="color:#ff00ff;">[</span></strong>maxn<strong><span style="color:#ff00ff;">];</span><span style="color:blue;">
int</span></strong> d<strong><span style="color:#ff00ff;">[</span></strong>maxn<strong><span style="color:#ff00ff;">];</span><span style="color:blue;">
int</span></strong> cur<strong><span style="color:#ff00ff;">[</span></strong>maxn<strong><span style="color:#ff00ff;">];</span><span style="color:blue;">
void</span></strong> init<strong><span style="color:#ff00ff;">()
{</span><span style="color:#0000ff;">
    for</span><span style="color:#ff00ff;">(</span><span style="color:blue;">int</span></strong> i<strong><span style="color:#ff00ff;">=</span></strong><span style="color:#cc3300;">0</span><strong><span style="color:#ff00ff;">;</span></strong>i<strong><span style="color:#ff00ff;"><=</span></strong>n<strong><span style="color:#ff00ff;">+</span></strong><span style="color:#cc3300;">1</span><strong><span style="color:#ff00ff;">;</span></strong>i<strong><span style="color:#ff00ff;">++)</span></strong>
        G<strong><span style="color:#ff00ff;">[</span></strong>i<strong><span style="color:#ff00ff;">].</span></strong>clear<strong><span style="color:#ff00ff;">();</span></strong>
    edges<strong><span style="color:#ff00ff;">.</span></strong>clear<strong><span style="color:#ff00ff;">();
}</span><span style="color:blue;">
void</span></strong> AddEdge<strong><span style="color:#ff00ff;">(</span><span style="color:blue;">int</span></strong> from<strong><span style="color:#ff00ff;">,</span><span style="color:blue;">int</span></strong> to<strong><span style="color:#ff00ff;">,</span><span style="color:blue;">int</span></strong> cap<strong><span style="color:#ff00ff;">)
{</span></strong>
    edges<strong><span style="color:#ff00ff;">.</span></strong>push_back<strong><span style="color:#ff00ff;">(</span></strong>Edge<strong><span style="color:#ff00ff;">(</span></strong>from<strong><span style="color:#ff00ff;">,</span></strong>to<strong><span style="color:#ff00ff;">,</span></strong>cap<strong><span style="color:#ff00ff;">,</span></strong><span style="color:#cc3300;">0</span><strong><span style="color:#ff00ff;">));</span></strong>
    edges<strong><span style="color:#ff00ff;">.</span></strong>push_back<strong><span style="color:#ff00ff;">(</span></strong>Edge<strong><span style="color:#ff00ff;">(</span></strong>to<strong><span style="color:#ff00ff;">,</span></strong>from<strong><span style="color:#ff00ff;">,</span></strong><span style="color:#cc3300;">0</span><strong><span style="color:#ff00ff;">,</span></strong><span style="color:#cc3300;">0</span><strong><span style="color:#ff00ff;">));</span><span style="color:blue;">
    int</span></strong> M<strong><span style="color:#ff00ff;"> =</span></strong> edges<strong><span style="color:#ff00ff;">.</span></strong>size<strong><span style="color:#ff00ff;">();</span></strong>
    G<strong><span style="color:#ff00ff;">[</span></strong>from<strong><span style="color:#ff00ff;">].</span></strong>push_back<strong><span style="color:#ff00ff;">(</span></strong>M<strong><span style="color:#ff00ff;">-</span></strong><span style="color:#cc3300;">2</span><strong><span style="color:#ff00ff;">);</span></strong>
    G<strong><span style="color:#ff00ff;">[</span></strong>to<strong><span style="color:#ff00ff;">].</span></strong>push_back<strong><span style="color:#ff00ff;">(</span></strong>M<strong><span style="color:#ff00ff;">-</span></strong><span style="color:#cc3300;">1</span><strong><span style="color:#ff00ff;">);
}</span><span style="color:blue;">
bool</span></strong> BFS<strong><span style="color:#ff00ff;">()
{</span></strong>
    memset<strong><span style="color:#ff00ff;">(</span></strong>vis<strong><span style="color:#ff00ff;">,</span></strong><span style="color:#cc3300;">0</span><strong><span style="color:#ff00ff;">,</span><span style="color:#0000ff;">sizeof</span><span style="color:#ff00ff;">(</span></strong>vis<strong><span style="color:#ff00ff;">));</span></strong>
    queue<strong><span style="color:#ff00ff;"><</span><span style="color:blue;">int</span><span style="color:#ff00ff;">></span></strong>Q<strong><span style="color:#ff00ff;">;</span></strong>
    Q<strong><span style="color:#ff00ff;">.</span></strong>push<strong><span style="color:#ff00ff;">(</span></strong>s<strong><span style="color:#ff00ff;">);</span></strong>
    d<strong><span style="color:#ff00ff;">[</span></strong>s<strong><span style="color:#ff00ff;">] =</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;">;</span></strong>
    vis<strong><span style="color:#ff00ff;">[</span></strong>s<strong><span style="color:#ff00ff;">] =</span></strong><span style="color:#cc3300;"> 1</span><strong><span style="color:#ff00ff;">;</span><span style="color:#0000ff;">
    while</span><span style="color:#ff00ff;">(!</span></strong>Q<strong><span style="color:#ff00ff;">.</span></strong>empty<strong><span style="color:#ff00ff;">())
    {</span><span style="color:blue;">
        int</span></strong> x<strong><span style="color:#ff00ff;"> =</span></strong> Q<strong><span style="color:#ff00ff;">.</span></strong>front<strong><span style="color:#ff00ff;">();</span></strong>Q<strong><span style="color:#ff00ff;">.</span></strong>pop<strong><span style="color:#ff00ff;">();</span><span style="color:#0000ff;">
        for</span><span style="color:#ff00ff;">(</span><span style="color:blue;">int</span></strong> i<strong><span style="color:#ff00ff;">=</span></strong><span style="color:#cc3300;">0</span><strong><span style="color:#ff00ff;">;</span></strong>i<strong><span style="color:#ff00ff;"><</span></strong>G<strong><span style="color:#ff00ff;">[</span></strong>x<strong><span style="color:#ff00ff;">].</span></strong>size<strong><span style="color:#ff00ff;">();</span></strong>i<strong><span style="color:#ff00ff;">++)
        {</span></strong>
            Edge<strong><span style="color:#ff00ff;">&</span></strong> e<strong><span style="color:#ff00ff;"> =</span></strong> edges<strong><span style="color:#ff00ff;">[</span></strong>G<strong><span style="color:#ff00ff;">[</span></strong>x<strong><span style="color:#ff00ff;">][</span></strong>i<strong><span style="color:#ff00ff;">]];</span><span style="color:#0000ff;">
            if</span><span style="color:#ff00ff;">(!</span></strong>vis<strong><span style="color:#ff00ff;">[</span></strong>e<strong><span style="color:#ff00ff;">.</span></strong>to<strong><span style="color:#ff00ff;">] &&</span></strong> e<strong><span style="color:#ff00ff;">.</span></strong>cap<strong><span style="color:#ff00ff;"> ></span></strong> e<strong><span style="color:#ff00ff;">.</span></strong>flow<strong><span style="color:#ff00ff;">)
            {</span></strong>
                vis<strong><span style="color:#ff00ff;">[</span></strong>e<strong><span style="color:#ff00ff;">.</span></strong>to<strong><span style="color:#ff00ff;">] =</span></strong><span style="color:#cc3300;"> 1</span><strong><span style="color:#ff00ff;">;</span></strong>
                d<strong><span style="color:#ff00ff;">[</span></strong>e<strong><span style="color:#ff00ff;">.</span></strong>to<strong><span style="color:#ff00ff;">] =</span></strong> d<strong><span style="color:#ff00ff;">[</span></strong>x<strong><span style="color:#ff00ff;">] +</span></strong><span style="color:#cc3300;"> 1</span><strong><span style="color:#ff00ff;">;</span></strong>
                Q<strong><span style="color:#ff00ff;">.</span></strong>push<strong><span style="color:#ff00ff;">(</span></strong>e<strong><span style="color:#ff00ff;">.</span></strong>to<strong><span style="color:#ff00ff;">);
            }
        }
    }</span><span style="color:#0000ff;">
    return</span></strong> vis<strong><span style="color:#ff00ff;">[</span></strong>t<strong><span style="color:#ff00ff;">];
}</span><span style="color:blue;">
int</span></strong> DFS<strong><span style="color:#ff00ff;">(</span><span style="color:blue;">int</span></strong> x<strong><span style="color:#ff00ff;">,</span><span style="color:blue;">int</span></strong> a<strong><span style="color:#ff00ff;">)
{</span><span style="color:#0000ff;">
    if</span><span style="color:#ff00ff;">(</span></strong>x<strong><span style="color:#ff00ff;"> ==</span></strong> t<strong><span style="color:#ff00ff;"> ||</span></strong> a<strong><span style="color:#ff00ff;"> ==</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;">)</span><span style="color:#0000ff;"> return</span></strong> a<strong><span style="color:#ff00ff;">;</span><span style="color:blue;">
    int</span></strong> flow<strong><span style="color:#ff00ff;"> =</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;"> ,</span></strong> f<strong><span style="color:#ff00ff;">;</span><span style="color:#0000ff;">
    for</span><span style="color:#ff00ff;">(</span><span style="color:blue;">int</span><span style="color:#ff00ff;"> &</span></strong>i<strong><span style="color:#ff00ff;"> =</span></strong> cur<strong><span style="color:#ff00ff;">[</span></strong>x<strong><span style="color:#ff00ff;">];</span></strong>i<strong><span style="color:#ff00ff;"><</span></strong>G<strong><span style="color:#ff00ff;">[</span></strong>x<strong><span style="color:#ff00ff;">].</span></strong>size<strong><span style="color:#ff00ff;">();</span></strong>i<strong><span style="color:#ff00ff;">++)
    {</span></strong>
        Edge<strong><span style="color:#ff00ff;">&</span></strong> e<strong><span style="color:#ff00ff;"> =</span></strong> edges<strong><span style="color:#ff00ff;">[</span></strong>G<strong><span style="color:#ff00ff;">[</span></strong>x<strong><span style="color:#ff00ff;">][</span></strong>i<strong><span style="color:#ff00ff;">]];</span><span style="color:#0000ff;">
        if</span><span style="color:#ff00ff;">(</span></strong>d<strong><span style="color:#ff00ff;">[</span></strong>x<strong><span style="color:#ff00ff;">] +</span></strong><span style="color:#cc3300;"> 1</span><strong><span style="color:#ff00ff;"> ==</span></strong> d<strong><span style="color:#ff00ff;">[</span></strong>e<strong><span style="color:#ff00ff;">.</span></strong>to<strong><span style="color:#ff00ff;">] && (</span></strong>f<strong><span style="color:#ff00ff;"> =</span></strong> DFS<strong><span style="color:#ff00ff;">(</span></strong>e<strong><span style="color:#ff00ff;">.</span></strong>to<strong><span style="color:#ff00ff;">,</span></strong>min<strong><span style="color:#ff00ff;">(</span></strong>a<strong><span style="color:#ff00ff;">,</span></strong>e<strong><span style="color:#ff00ff;">.</span></strong>cap<strong><span style="color:#ff00ff;">-</span></strong>e<strong><span style="color:#ff00ff;">.</span></strong>flow<strong><span style="color:#ff00ff;">))) ></span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;">)
        {</span></strong>
            e<strong><span style="color:#ff00ff;">.</span></strong>flow<strong><span style="color:#ff00ff;"> +=</span></strong> f<strong><span style="color:#ff00ff;">;</span></strong>
            edges<strong><span style="color:#ff00ff;">[</span></strong>G<strong><span style="color:#ff00ff;">[</span></strong>x<strong><span style="color:#ff00ff;">][</span></strong>i<strong><span style="color:#ff00ff;">]^</span></strong><span style="color:#cc3300;">1</span><strong><span style="color:#ff00ff;">].</span></strong>flow<strong><span style="color:#ff00ff;"> -=</span></strong> f<strong><span style="color:#ff00ff;">;</span></strong>
            flow<strong><span style="color:#ff00ff;"> +=</span></strong> f<strong><span style="color:#ff00ff;">;</span></strong>
            a<strong><span style="color:#ff00ff;"> -=</span></strong> f<strong><span style="color:#ff00ff;">;</span><span style="color:#0000ff;">
            if</span><span style="color:#ff00ff;">(</span></strong>a<strong><span style="color:#ff00ff;"> ==</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;">)</span><span style="color:#0000ff;"> break</span><span style="color:#ff00ff;">;
        }
    }</span><span style="color:#0000ff;">
    return</span></strong> flow<strong><span style="color:#ff00ff;">;
}</span><span style="color:blue;">
int</span></strong> Maxflow<strong><span style="color:#ff00ff;">()
{</span><span style="color:blue;">
    int</span></strong> flow<strong><span style="color:#ff00ff;"> =</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;">;</span><span style="color:#0000ff;">
    while</span><span style="color:#ff00ff;">(</span></strong>BFS<strong><span style="color:#ff00ff;">())
    {</span></strong>
        memset<strong><span style="color:#ff00ff;">(</span></strong>cur<strong><span style="color:#ff00ff;">,</span></strong><span style="color:#cc3300;">0</span><strong><span style="color:#ff00ff;">,</span><span style="color:#0000ff;">sizeof</span><span style="color:#ff00ff;">(</span></strong>cur<strong><span style="color:#ff00ff;">));</span></strong>
        flow<strong><span style="color:#ff00ff;"> +=</span></strong> DFS<strong><span style="color:#ff00ff;">(</span></strong>s<strong><span style="color:#ff00ff;">,</span></strong>INF<strong><span style="color:#ff00ff;">);
    }</span><span style="color:#0000ff;">
    return</span></strong> flow<strong><span style="color:#ff00ff;">;
}</span><span style="color:blue;">
int</span></strong> pre<strong><span style="color:#ff00ff;">[</span></strong>maxn<strong><span style="color:#ff00ff;">] ,</span></strong> lowlink<strong><span style="color:#ff00ff;">[</span></strong>maxn<strong><span style="color:#ff00ff;">] ,</span></strong>sccno<strong><span style="color:#ff00ff;">[</span></strong>maxn<strong><span style="color:#ff00ff;">] ,</span></strong> fa<strong><span style="color:#ff00ff;">[</span></strong>maxn<strong><span style="color:#ff00ff;">],</span></strong> dfs_clock<strong><span style="color:#ff00ff;"> ,</span></strong> scc_cnt<strong><span style="color:#ff00ff;"> ,</span></strong> flag<strong><span style="color:#ff00ff;">;</span></strong>
stack<strong><span style="color:#ff00ff;"><</span><span style="color:blue;">int</span><span style="color:#ff00ff;">></span></strong> S<strong><span style="color:#ff00ff;">;</span><span style="color:blue;">
void</span></strong> dfs<strong><span style="color:#ff00ff;">(</span><span style="color:blue;">int</span></strong> u<strong><span style="color:#ff00ff;">)
{</span></strong>
    pre<strong><span style="color:#ff00ff;">[</span></strong>u<strong><span style="color:#ff00ff;">] =</span></strong> lowlink<strong><span style="color:#ff00ff;">[</span></strong>u<strong><span style="color:#ff00ff;">] = ++</span></strong>dfs_clock<strong><span style="color:#ff00ff;">;</span></strong>
    S<strong><span style="color:#ff00ff;">.</span></strong>push<strong><span style="color:#ff00ff;">(</span></strong>u<strong><span style="color:#ff00ff;">);</span><span style="color:#0000ff;">
    for</span><span style="color:#ff00ff;">(</span><span style="color:blue;">int</span></strong> i<strong><span style="color:#ff00ff;">=</span></strong><span style="color:#cc3300;">0</span><strong><span style="color:#ff00ff;">;</span></strong>i<strong><span style="color:#ff00ff;"><</span></strong>G<strong><span style="color:#ff00ff;">[</span></strong>u<strong><span style="color:#ff00ff;">].</span></strong>size<strong><span style="color:#ff00ff;">();</span></strong>i<strong><span style="color:#ff00ff;">++)
    {</span></strong>
        Edge<strong><span style="color:#ff00ff;">&</span></strong> e<strong><span style="color:#ff00ff;"> =</span></strong> edges<strong><span style="color:#ff00ff;">[</span></strong>G<strong><span style="color:#ff00ff;">[</span></strong>u<strong><span style="color:#ff00ff;">][</span></strong>i<strong><span style="color:#ff00ff;">]];</span><span style="color:#0000ff;">
        if</span><span style="color:#ff00ff;">(</span></strong>e<strong><span style="color:#ff00ff;">.</span></strong>cap<strong><span style="color:#ff00ff;"> ==</span></strong> e<strong><span style="color:#ff00ff;">.</span></strong>flow<strong><span style="color:#ff00ff;">)</span><span style="color:#0000ff;"> continue</span><span style="color:#ff00ff;">;</span><span style="color:blue;">
        int</span></strong> v<strong><span style="color:#ff00ff;"> =</span></strong> e<strong><span style="color:#ff00ff;">.</span></strong>to<strong><span style="color:#ff00ff;">;</span><span style="color:#0000ff;">
        if</span><span style="color:#ff00ff;">(!</span></strong>pre<strong><span style="color:#ff00ff;">[</span></strong>v<strong><span style="color:#ff00ff;">])
        {</span></strong>
            fa<strong><span style="color:#ff00ff;">[</span></strong>v<strong><span style="color:#ff00ff;">] =</span></strong> u<strong><span style="color:#ff00ff;">;</span></strong>
            dfs<strong><span style="color:#ff00ff;">(</span></strong>v<strong><span style="color:#ff00ff;">);</span><span style="color:#0000ff;">
            if</span><span style="color:#ff00ff;">(!</span></strong>flag<strong><span style="color:#ff00ff;">)</span><span style="color:#0000ff;"> return</span><span style="color:#ff00ff;"> ;</span></strong>
            lowlink<strong><span style="color:#ff00ff;">[</span></strong>u<strong><span style="color:#ff00ff;">] =</span></strong> min<strong><span style="color:#ff00ff;">(</span></strong>lowlink<strong><span style="color:#ff00ff;">[</span></strong>u<strong><span style="color:#ff00ff;">] ,</span></strong> lowlink<strong><span style="color:#ff00ff;">[</span></strong>v<strong><span style="color:#ff00ff;">]);
        }</span><span style="color:#0000ff;">
        else if</span><span style="color:#ff00ff;">(!</span></strong>sccno<strong><span style="color:#ff00ff;">[</span></strong>v<strong><span style="color:#ff00ff;">])
        {</span></strong>
         lowlink<strong><span style="color:#ff00ff;">[</span></strong>u<strong><span style="color:#ff00ff;">] =</span></strong> min<strong><span style="color:#ff00ff;">(</span></strong>lowlink<strong><span style="color:#ff00ff;">[</span></strong>u<strong><span style="color:#ff00ff;">] ,</span></strong> pre<strong><span style="color:#ff00ff;">[</span></strong>v<strong><span style="color:#ff00ff;">]);</span><span style="color:#0000ff;">
         if</span><span style="color:#ff00ff;">(</span></strong>v<strong><span style="color:#ff00ff;"> !=</span></strong> fa<strong><span style="color:#ff00ff;">[</span></strong>u<strong><span style="color:#ff00ff;">]) {</span></strong>flag<strong><span style="color:#ff00ff;"> =</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;"> ;</span><span style="color:#0000ff;"> return</span><span style="color:#ff00ff;"> ;}
        }
    }</span><span style="color:#0000ff;">
    if</span><span style="color:#ff00ff;">(</span></strong>lowlink<strong><span style="color:#ff00ff;">[</span></strong>u<strong><span style="color:#ff00ff;">] ==</span></strong> pre<strong><span style="color:#ff00ff;">[</span></strong>u<strong><span style="color:#ff00ff;">])
    {</span></strong>
        scc_cnt<strong><span style="color:#ff00ff;">++;</span><span style="color:#0000ff;">
        for</span><span style="color:#ff00ff;">(;;)
        {</span><span style="color:blue;">
            int</span></strong> x<strong><span style="color:#ff00ff;"> =</span></strong> S<strong><span style="color:#ff00ff;">.</span></strong>top<strong><span style="color:#ff00ff;">();</span></strong> S<strong><span style="color:#ff00ff;">.</span></strong>pop<strong><span style="color:#ff00ff;">();</span></strong>
            sccno<strong><span style="color:#ff00ff;">[</span></strong>x<strong><span style="color:#ff00ff;">] =</span></strong> scc_cnt<strong><span style="color:#ff00ff;">;</span><span style="color:#0000ff;">
            if</span><span style="color:#ff00ff;">(</span></strong>x<strong><span style="color:#ff00ff;"> ==</span></strong> u<strong><span style="color:#ff00ff;">)</span><span style="color:#0000ff;"> break</span><span style="color:#ff00ff;">;
        }
    }
}</span><span style="color:blue;">
bool</span></strong> find_scc<strong><span style="color:#ff00ff;">()
{</span></strong>
    dfs_clock<strong><span style="color:#ff00ff;"> =</span></strong> scc_cnt<strong><span style="color:#ff00ff;"> =</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;">;</span></strong> flag<strong><span style="color:#ff00ff;"> =</span></strong><span style="color:#cc3300;"> 1</span><strong><span style="color:#ff00ff;">;</span></strong>
    memset<strong><span style="color:#ff00ff;">(</span></strong>pre<strong><span style="color:#ff00ff;"> ,</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;">,</span><span style="color:#0000ff;"> sizeof</span><span style="color:#ff00ff;">(</span></strong>pre<strong><span style="color:#ff00ff;">));</span></strong>
    memset<strong><span style="color:#ff00ff;">(</span></strong>lowlink<strong><span style="color:#ff00ff;"> ,</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;">,</span><span style="color:#0000ff;"> sizeof</span><span style="color:#ff00ff;">(</span></strong>lowlink<strong><span style="color:#ff00ff;">));</span></strong>
    memset<strong><span style="color:#ff00ff;">(</span></strong>sccno<strong><span style="color:#ff00ff;"> ,</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;"> ,</span><span style="color:#0000ff;"> sizeof</span><span style="color:#ff00ff;">(</span></strong>sccno<strong><span style="color:#ff00ff;">));</span><span style="color:#0000ff;">
    for</span><span style="color:#ff00ff;">(</span><span style="color:blue;">int</span></strong> i<strong><span style="color:#ff00ff;">=</span></strong><span style="color:#cc3300;">0</span><strong><span style="color:#ff00ff;">;</span></strong>i<strong><span style="color:#ff00ff;"><</span></strong>n<strong><span style="color:#ff00ff;">;</span></strong>i<strong><span style="color:#ff00ff;">++)</span><span style="color:#0000ff;">
    if</span><span style="color:#ff00ff;">(!</span></strong>pre<strong><span style="color:#ff00ff;">[</span></strong>i<strong><span style="color:#ff00ff;">]) {</span></strong> dfs<strong><span style="color:#ff00ff;">(</span></strong>i<strong><span style="color:#ff00ff;">);</span><span style="color:#0000ff;"> if</span><span style="color:#ff00ff;">(!</span></strong>flag<strong><span style="color:#ff00ff;">)</span><span style="color:#0000ff;"> return</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;">;}</span><span style="color:#0000ff;">
    return</span></strong><span style="color:#cc3300;"> 1</span><strong><span style="color:#ff00ff;">;
}</span><span style="color:blue;">
int</span><span style="color:#0000ff;"> main</span><span style="color:#ff00ff;">()
{</span><span style="color:blue;">
    int</span></strong> T<strong><span style="color:#ff00ff;"> ,</span></strong> kcase<strong><span style="color:#ff00ff;"> =</span></strong><span style="color:#cc3300;"> 1</span><strong><span style="color:#ff00ff;">;</span><span style="color:blue;">
    int</span></strong> N<strong><span style="color:#ff00ff;"> ,</span></strong> M<strong><span style="color:#ff00ff;"> ,</span></strong> K<strong><span style="color:#ff00ff;">;</span></strong>
    scanf<strong><span style="color:#ff00ff;">(</span></strong><span style="color:green;">"%d"</span><strong><span style="color:#ff00ff;">,&</span></strong>T<strong><span style="color:#ff00ff;">);</span><span style="color:#0000ff;">
    while</span><span style="color:#ff00ff;">(</span></strong>T<strong><span style="color:#ff00ff;">--)
    {</span></strong>
        scanf<strong><span style="color:#ff00ff;">(</span></strong><span style="color:green;">"%d%d"</span><strong><span style="color:#ff00ff;">,&</span></strong>N<strong><span style="color:#ff00ff;">,&</span></strong>M<strong><span style="color:#ff00ff;">);</span></strong>
        K<strong><span style="color:#ff00ff;"> =</span></strong><span style="color:#cc3300;"> 9</span><strong><span style="color:#ff00ff;">;</span></strong>s<strong><span style="color:#ff00ff;"> =</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;"> ,</span></strong> t<strong><span style="color:#ff00ff;"> =</span></strong> N<strong><span style="color:#ff00ff;"> +</span></strong> M<strong><span style="color:#ff00ff;"> +</span></strong><span style="color:#cc3300;"> 1</span><strong><span style="color:#ff00ff;">;</span></strong>
        n<strong><span style="color:#ff00ff;"> =</span></strong> t<strong><span style="color:#ff00ff;"> +</span></strong><span style="color:#cc3300;"> 1</span><strong><span style="color:#ff00ff;">;</span></strong> init<strong><span style="color:#ff00ff;">();</span><span style="color:blue;">
        int</span></strong> sumr<strong><span style="color:#ff00ff;"> =</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;"> ,</span></strong> sumc<strong><span style="color:#ff00ff;"> =</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;">;</span><span style="color:blue;">
        int</span></strong> x<strong><span style="color:#ff00ff;">;</span><span style="color:#0000ff;">
        for</span><span style="color:#ff00ff;">(</span><span style="color:blue;">int</span></strong> i<strong><span style="color:#ff00ff;">=</span></strong><span style="color:#cc3300;">1</span><strong><span style="color:#ff00ff;">;</span></strong>i<strong><span style="color:#ff00ff;"><=</span></strong>N<strong><span style="color:#ff00ff;">;</span></strong>i<strong><span style="color:#ff00ff;">++)
        {</span></strong>
            scanf<strong><span style="color:#ff00ff;">(</span></strong><span style="color:green;">"%d"</span><strong><span style="color:#ff00ff;">,&</span></strong>x<strong><span style="color:#ff00ff;">);</span></strong>
            AddEdge<strong><span style="color:#ff00ff;">(</span></strong>s<strong><span style="color:#ff00ff;"> ,</span></strong> i<strong><span style="color:#ff00ff;"> ,</span></strong> x<strong><span style="color:#ff00ff;">);</span></strong>
            sumr<strong><span style="color:#ff00ff;"> +=</span></strong> x<strong><span style="color:#ff00ff;">;
        }</span><span style="color:#0000ff;">
        for</span><span style="color:#ff00ff;">(</span><span style="color:blue;">int</span></strong> i<strong><span style="color:#ff00ff;">=</span></strong><span style="color:#cc3300;">1</span><strong><span style="color:#ff00ff;">;</span></strong>i<strong><span style="color:#ff00ff;"><=</span></strong>M<strong><span style="color:#ff00ff;">;</span></strong>i<strong><span style="color:#ff00ff;">++)
        {</span></strong>
            scanf<strong><span style="color:#ff00ff;">(</span></strong><span style="color:green;">"%d"</span><strong><span style="color:#ff00ff;">,&</span></strong>x<strong><span style="color:#ff00ff;">);</span></strong>
            AddEdge<strong><span style="color:#ff00ff;">(</span></strong>i<strong><span style="color:#ff00ff;"> +</span></strong> N<strong><span style="color:#ff00ff;"> ,</span></strong> t<strong><span style="color:#ff00ff;"> ,</span></strong> x<strong><span style="color:#ff00ff;">);</span></strong>
            sumc<strong><span style="color:#ff00ff;"> +=</span></strong> x<strong><span style="color:#ff00ff;">;
        }</span></strong>
        printf<strong><span style="color:#ff00ff;">(</span></strong><span style="color:green;">"Case #%d: "</span><strong><span style="color:#ff00ff;">,</span></strong>kcase<strong><span style="color:#ff00ff;">++);</span><span style="color:#0000ff;">
        if</span><span style="color:#ff00ff;"> (</span></strong>sumr<strong><span style="color:#ff00ff;"> !=</span></strong> sumc<strong><span style="color:#ff00ff;">){</span></strong> printf<strong><span style="color:#ff00ff;">(</span></strong><span style="color:green;">"So naive!\n"</span><strong><span style="color:#ff00ff;">);</span><span style="color:#0000ff;">continue</span><span style="color:#ff00ff;">; }</span><span style="color:#0000ff;">
        for</span><span style="color:#ff00ff;">(</span><span style="color:blue;">int</span></strong> i<strong><span style="color:#ff00ff;">=</span></strong><span style="color:#cc3300;">1</span><strong><span style="color:#ff00ff;">;</span></strong>i<strong><span style="color:#ff00ff;"><=</span></strong>N<strong><span style="color:#ff00ff;">;</span></strong>i<strong><span style="color:#ff00ff;">++)
        {</span><span style="color:#0000ff;">
            for</span><span style="color:#ff00ff;">(</span><span style="color:blue;">int</span></strong> j<strong><span style="color:#ff00ff;">=</span></strong><span style="color:#cc3300;">1</span><strong><span style="color:#ff00ff;">;</span></strong>j<strong><span style="color:#ff00ff;"><=</span></strong>M<strong><span style="color:#ff00ff;">;</span></strong>j<strong><span style="color:#ff00ff;">++)
            {</span></strong>
                AddEdge<strong><span style="color:#ff00ff;">(</span></strong>i<strong><span style="color:#ff00ff;"> ,</span></strong> j<strong><span style="color:#ff00ff;"> +</span></strong> N<strong><span style="color:#ff00ff;"> ,</span></strong> K<strong><span style="color:#ff00ff;">);
            }
        }</span><span style="color:blue;">
        int</span></strong> ans<strong><span style="color:#ff00ff;"> =</span></strong> Maxflow<strong><span style="color:#ff00ff;">();</span><span style="color:#0000ff;">
        if</span><span style="color:#ff00ff;">(</span></strong>ans<strong><span style="color:#ff00ff;"> !=</span></strong> sumr<strong><span style="color:#ff00ff;">) {</span></strong> printf<strong><span style="color:#ff00ff;">(</span></strong><span style="color:green;">"So naive!\n"</span><strong><span style="color:#ff00ff;">);</span><span style="color:#0000ff;"> continue</span><span style="color:#ff00ff;">; }</span><span style="color:#0000ff;">
        if</span><span style="color:#ff00ff;">(</span></strong>find_scc<strong><span style="color:#ff00ff;">())</span></strong> printf<strong><span style="color:#ff00ff;">(</span></strong><span style="color:green;">"So simple!\n"</span><strong><span style="color:#ff00ff;">);</span><span style="color:#0000ff;">
        else</span></strong> printf<strong><span style="color:#ff00ff;">(</span></strong><span style="color:green;">"So young!\n"</span><strong><span style="color:#ff00ff;">);
    }</span><span style="color:#0000ff;">
    return</span></strong><span style="color:#cc3300;"> 0</span><strong><span style="color:#ff00ff;">;
}</span></strong>



时间: 2024-10-08 08:53:10

HDOJ 4888 Redraw Beautiful Drawings && HDOJ 4975 A simple Gaussian elimination problem的相关文章

hdoj 4888 Redraw Beautiful Drawings 【最大流满流+唯一性判断】

题目:hdoj 4888 Redraw Beautiful Drawings 分类:最大流满流 , 最大流唯一性 来源:2014 Multi-University Training Contest 3 题意:一个矩阵的每行每列的和都知道,然后让你求能不能填,是否唯一,唯一的话输出解. 分析:这个题目能看出来是最大流,但是难点有2. 首先:题中矩阵400 * 400 ,好在题目中矩阵和没有特殊要求,所以可以直接以行列建图,建图方案: 1. 源点 -> 每一行对应的点,流量限制为该行的和 2. 每一

hdoj 4975 A simple Gaussian elimination problem. 【最大流唯一性判断】

题目:hdoj 4975 A simple Gaussian elimination problem. 这个题目跟hdoj 4888 一样,只是数据加强了一点,这个题目确实出的不好,尤其数据,争议比较大,但是同时也说明优化有时候还是很有用的. 不懂的可以看这个讲解:点击 这个题目只是加了一点优化,就是判断的时候加入是行和为0,或者满的话,就跳出不用判断,然后就300ms过了.真心牛 AC代码: #include <cstdio> #include <cstring> #includ

hdu 4975 A simple Gaussian elimination problem.(网络流,判断矩阵是否存在)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One day, he drew a table with several rows and columns, randomly wrote numbers on each elements of the table. Then he counted the sum of each row and col

HDU 4975 A simple Gaussian elimination problem.(网络最大流)

http://acm.hdu.edu.cn/showproblem.php?pid=4975 A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 669    Accepted Submission(s): 222 Problem Description Drag

HDU 4975 A simple Gaussian elimination problem. 网络流+矩阵上的dp

随机输出保平安啊 和hdu4888一个意思,先跑个网络流然后dp判可行. ==n^3的dp过不了,所以把n改成200. ==因为出题人没有把多解的情况放在200*200以外的矩阵. #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; const int MAX_N = 12

HDOJ 4888 Redraw Beautiful Drawings

最大流判断多解 建图: 源点连接到每一个代表行的节点容量为行总和,每一个代表列的节点连接到汇点容量为列总和,行和列之间互相连接容量为Limit 多解: 做一遍ISAP后,在残量图上DFS看能否找到点数大于2的环即可 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3519    Acc

HDOJ 4975 A simple Gaussian elimination problem.

和HDOJ4888是一样的问题,最大流推断多解 1.把ISAP卡的根本出不来结果,仅仅能把全为0或者全为满流的给特判掉...... 2.在残量网络中找大于2的圈要用一种类似tarjian的方法从汇点開始找,推断哪些点没有到汇点 A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submiss

HDU 4975 A simple Gaussian elimination problem.

http://acm.hdu.edu.cn/showproblem.php?pid=4975 题意:同HDU 4888.给N行M列,每行之和,每列之和,判断矩阵是不是唯一. 题解:网络流.源点和每行之和建边,容量为和:汇点和没列之和建边,容量为和:行之和和列之和建边,容量为9(每位只能是0~9). 判断可行性:行之和的和是否等于列之和的和:是否满流. 判断唯一解:残留网络里是否有长度大于等于2的环 1 #include <cstdio> 2 #include <cstring> 3

hdu - 4975 - A simple Gaussian elimination problem.(最大流)

题意:给一个N行M列的数字矩阵的行和以及列和,每个元素的大小不超过9,问这样的矩阵是否存在,是否唯一N(1 ≤ N ≤ 500) , M(1 ≤ M ≤ 500). 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 -->>方法如:http://blog.csdn.net/scnu_jiechao/article/details/40658221 先做hdu - 4888,再来做此题的时候,感觉这题好 SB 呀,将代码提交后自己就 SB 了