zoukankan      html  css  js  c++  java
  • 考研路茫茫——空调教室HDU2242(Tarjan缩点)

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=2242

    给你一个图,问你缩完点树上割边的做小绝对值差。

    思路:

    这题核算起来整整做了我一天(即24个小时)!!!一开始是MLE了近20发,然后TLE5、6发,再WA了一个晚上加一个下午。

    有一种自闭是你突然对任何事物都失去追求:期中考?0分算了,这题再错我不学了还不行吗。

    再加上HDU的评测机本来就很迷,该RE的判MLE,我N开1还MLE。服了嗷 !

    算法里要注意的就是:Tarjan里因为你补了一条反向边,所以edge^1和edge都标记掉不要再dfs了。

    然后Head链式前向星数组必须和tot变量初始化一致 -1 or 0,i!=-1 or 0,不然就炸了。(幸亏我同学帮我找到错误,不然我一个礼拜都难受)

      1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
      2 #include <cstdio>//sprintf islower isupper
      3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
      4 #include <iostream>//pair
      5 #include <fstream>//freopen("C:\Users\13606\Desktop\Input.txt","r",stdin);
      6 #include <bitset>
      7 #include <map>
      8 #include<unordered_map>
      9 #include <vector>
     10 #include <Stack>
     11 #include <set>
     12 #include <string.h>//strstr substr
     13 #include <string>
     14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
     15 #include <cmath>
     16 #include <deque>
     17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
     18 #include <vector>//emplace_back
     19 //#include <math.h>
     20 #include <cassert>
     21 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
     22 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
     23 #include <iomanip>
     24 
     25 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
     26 //******************
     27 clock_t __STRAT,__END;
     28 double __TOTALTIME;
     29 void _MS(){__STRAT=clock();}
     30 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
     31 //***********************
     32 #define rint register int
     33 #define fo(a,b,c) for(rint a=b;a<=c;++a)
     34 #define fr(a,b,c) for(rint a=b;a>=c;--a)
     35 #define mem(a,b) memset(a,b,sizeof(a))
     36 #define pr printf
     37 #define sc scanf
     38 #define ls rt<<1
     39 #define rs rt<<1|1
     40 typedef pair<int,int> PII;
     41 typedef vector<int> VI;
     42 typedef long long ll;
     43 const double E=2.718281828;
     44 const double PI=acos(-1.0);
     45 const ll INF=(1LL<<60);
     46 const int inf=(1<<30);
     47 const double ESP=1e-9;
     48 const int mod=(int)1e9+7;
     49 const int N=(int)40004;
     50 
     51 int val[N];
     52 int peo[N];
     53 int dfn[N],low[N],Stack[N],color[N];
     54 bool vis[N];
     55 int deep,top,sccn;//sccn是团;
     56 int tot,head[N];//edge;
     57 void Init()
     58 {
     59     tot=-1;
     60     deep=sccn=0;
     61     top=0;
     62     mem(Stack,0);
     63     mem(low,0);
     64     mem(dfn,0);
     65     mem(vis,0);
     66     mem(head,-1);
     67     mem(val,0);
     68 }
     69 struct node
     70 {
     71     int next,to;
     72     bool vis;
     73 }edge[N];
     74 void add(int from,int to)
     75 {
     76     ++tot;
     77     edge[tot].to=to;
     78     edge[tot].next=head[from];
     79     head[from]=tot;
     80 }
     81 void Tarjan(int u,int fa)
     82 {
     83     dfn[u]=++deep;
     84     low[u]=deep;
     85     vis[u]=true;
     86     Stack[++top]=u;
     87     for(int i=head[u];i!=-1;i=edge[i].next)
     88     {
     89         int to=edge[i].to;
     90         if(edge[i].vis) continue;
     91         edge[i].vis=edge[i^1].vis=1;
     92         if(!dfn[to])
     93         {
     94             Tarjan(to,u);
     95             low[u]=min(low[u],low[to]);
     96         }
     97         else if(vis[to])
     98             low[u]=min(low[u],low[to]);
     99     }
    100     if(dfn[u]==low[u])
    101     {
    102         ++sccn;
    103         do{
    104             vis[Stack[top]]=false;
    105             color[Stack[top]]=sccn;
    106             val[sccn]+=peo[Stack[top]];
    107         }while(Stack[top--]!=u);
    108     }
    109 }
    110 struct
    111 {
    112     int u,v;
    113 }e[N];
    114 int ans;
    115 int sum;
    116 int dfs(int u,int fa)
    117 {
    118     int temp=val[u];
    119     vis[u]=1;
    120     for(int i=head[u];i;i=edge[i].next)
    121     {
    122         int to=edge[i].to;
    123         if(to!=fa&&!vis[to])
    124         {
    125             temp+=dfs(to,u);
    126         }
    127     }
    128     ans=min(ans,abs(sum-temp*2));
    129     return temp;
    130 }
    131 
    132 int main()
    133 {
    134     int n,m;
    135     while(~sc("%d%d",&n,&m))
    136     {
    137         Init();
    138         sum=0;
    139         for(int i=1;i<=n;++i)
    140             sc("%d",&peo[i]),sum+=peo[i];
    141         mem(edge,0);
    142         for(int i=1;i<=m;++i)
    143         {
    144             int u,v;
    145             sc("%d%d",&u,&v);
    146             u++,v++;
    147             e[i]={u,v};
    148             add(u,v);
    149             add(v,u);
    150         }
    151         for(int i=1;i<=n;++i)
    152             if(!dfn[i])
    153                 Tarjan(i,-1);
    154         if(sccn==1)
    155         {
    156             pr("impossible
    ");
    157             continue;
    158         }
    159         ans=2e9;
    160         mem(head,0);
    161         tot=0;
    162         mem(edge,0);
    163         for(int i=1;i<=m;++i)
    164         {
    165             if(color[e[i].u]==color[e[i].v])continue;
    166             add(color[e[i].u],color[e[i].v]);
    167             add(color[e[i].v],color[e[i].u]);
    168         }
    169         mem(vis,0);
    170         dfs(1,0);
    171         pr("%d
    ",ans);
    172     }
    173     return 0;
    174 }
    175 
    176 /**************************************************************************************/
  • 相关阅读:
    PHP学习笔记(二)
    PHP开发笔记(一)
    PHP NOTE
    python windows进制文件可以直接下载使用
    深入浅出Hyper-V网络虚拟化技术
    深入浅出Hyper-V网络虚拟化(序)
    Windows Server 笔记(七):Windows Server 2012 R2 NIC Teaming(NIC组)
    Hyper-v Server 2012 R2增强会话模式
    Hyper-V动态迁移中?小心性能损失
    CLOUDSTACK FOR HYPER-V
  • 原文地址:https://www.cnblogs.com/--HPY-7m/p/11813992.html
Copyright © 2011-2022 走看看