[Graph theory] [Title] blue brush title

1> Neural Networks

Green title

Why topology?

1) necessary conditions: DAG
2) the formula of this title, the show needs to be calculated can be connected to this point, all of the boundary * point value
and therefore when I started to push this point, the status of all of his pioneer point of all requirements finish
this can only be topo_sort

#include<cstdio>
#include<cstdlib>
#include<queue>
#include<vector>
using namespace std;
int n,m;
const int N=103;
int c[N],in[N],sz[N];
struct node
{
    int v,w;
    node(int vv,int ww)
    { v=vv,w=ww; }
    node(){}
};
vector <node > g[N];
queue <int > q;

void topo_sort()
{
    while(!q.empty())
    {
        int t=q.front();q.pop();
        sz[t]=g[t].size() ;
        
        for(int i=0;i<sz[t];i++)
        {
            node v=g[t][i];
            if(c[t]>0) c[v.v ]+=c[t]*v.w ;
            if(--in[v.v ]==0) q.push(v.v );
        }
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    int x;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&c[i],&x);
        if(c[i]>0) q.push(i);
        else c[i]-=x;
    }
    int u,v,w;
    while(m--)
    {
        scanf("%d%d%d",&u,&v,&w);
        g[u].push_back(node(v,w)); 
        in[v]++;
    }
    
    topo_sort();
    bool fail=true;
    for(int i=1;i<=n;i++)
        if(!sz[i] && c[i]>0)
        {
            printf("%d %d\n",i,c[i]);
            fail=false;
        }
    if(fail) printf("NULL\n");
    return 0;
} 

 

2> Campus Network

> Difficulties:
the DAG, and how the minimum-side, so that all sides in the drawing a strongly connected component
> try to analyze the problem in terms of in, out, and
edged: +1 penetration, the degree of +1
> speculative :
any point after condensing digraph
to add some edge thereto, such that only in a strongly connected component,
adding the number of edges min = max (0-degree point, a point of 0 degree)
> proof: (luogu)

The DAG an arbitrary, must be formed by a plurality of chains, there must be a starting point and an end point v u

We first establish u-> v the edge, ah a little chain, became the point,

The point -1 is 0, a 0 degree point -1

Repeat the above steps,

 

Until finally all of the degree zero point, the zero degree point, are gone,

Figure Look, there is no chain, the next ring is, or that last only a strongly connected components

#include<cstdio>
#include<cstdlib>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;

int n,sum;
const int N=10003;
vector <int > g[N];
int fa[N],sz[N],dfn[N],low[N],tt;
stack <int> s;
bool f[N];
void tarjan (int x)
{
    dfn[x]=low[x]=++tt;
    f[x]=true,s.push(x);
    
    sz[x]=g[x].size() ;
    for(int i=0;i<sz[x];i++)
    {
        int v=g[x][i];
        if(!dfn[v])
        {
            tarjan(v);
            low[x]=min(low[x],low[v]);
        }
        else if(f[v])
            low[x]=min(low[x],low[v]);
    }
    
    if(dfn[x]==low[x])
    {
        ++sum;
        while(s.top()!=x)
            f[s.top()]=false,fa[s.top()]=sum,s.pop();
        
        f[x]=false,fa[x]=sum,s.pop() ;
    }
}

int in[N],out[N];//存sum节点的入度 
int main()
{
    scanf("%d",&n);
    int x;
    for(int i=1;i<=n;i++)
        while(~scanf("%d",&x) && x)
            g[i].push_back(x); 
    
    for(int i=1;i<=n;i++)
        if(!dfn[i]) tarjan(i);
    for(int i=1;i<=n;i++)
    {
        int fu=fa[i],fv;
        for(int j=0;j<sz[i];j++)
        {
            int v=g[i][j];
            fv=fa[v];
            if(fu!=fv) in[fv]++,out[fu]++;
        }
    }
    
    int ans1=0,ans2=0;
    for(int i=1;i<=sum;i++)
    {
        if(!in[i]) ans1++;
        if(!out[i]) ans2++; 
    }
    printf("%d\n%d",ans1,max(ans1,ans2));
    
    return 0;
}

 

3> travel plans

 

Guess you like

Origin www.cnblogs.com/xwww666666/p/11420155.html