Codeforces Round #375 (Div. 2)E. One-Way Reform

来源:互联网 发布:雅可比矩阵的逆矩阵 编辑:程序博客网 时间:2024/06/06 02:23

E. One-Way Reform  

</pre><p></p><p><span style="font-size:24px"><strong>      </strong><strong><span style="font-family:Microsoft YaHei;">题目大意:给定一个无向图,然后给边定向,使得入度等于出度的点最多。<span style="white-space:pre"></span>答案的个数很好确定,即度数为偶数的点的个数,下面考虑如何给边定向<span style="white-space:pre"></span>     首先想到的是跑一遍欧拉回路,但欧拉回路存在的条件是每个点的度为偶数,</span></strong></span></p><p><span style="font-size:24px"><strong><span style="font-family:Microsoft YaHei;">那对于度数为奇数的点怎么办?    <span style="white-space: pre;"> </span>由于度数为奇数的点的个数为偶数个(一条边产生两个度),所以可以把它们两两相连<span style="white-space:pre"></span>     但一个更简单的做法是把所有度为奇数的点向第n+1个点连一条边,这样,所有点的度就都为偶数了<span style="white-space:pre"></span>最后,对于每个点(图可能不联通)跑欧拉回路,给边定向,输出时注意只输出原图中存在的边 总结:抽象欧拉回路模型<span style="white-space:pre"></span>反思:逐步分析,注意细节</span></strong></span></p><p><span style="font-size:24px"><strong></strong></span></p><pre name="code" class="html">program O_O; var  i,j,k,n,m,cnt,ans,t:longint;  d:array[0..30000] of longint;  pr:array[0..40000,1..2]of longint;  vis:array[0..40000] of boolean;  a:array[1..400000] of record                     v:longint;             head,next:longint;             end;procedure adde(x,y:longint);begin  inc(cnt);  a[cnt].v:=y;  a[cnt].next:=a[x].head;  a[x].head:=cnt;end;procedure init; var  i,x,y:longint;begin  ans:=0;cnt:=1;  fillchar(vis,sizeof(vis),0);  fillchar(d,sizeof(d),0);  fillchar(a,sizeof(a),0);  readln(n,m);  for i:=1 to m do   begin    readln(x,y);    adde(x,y);adde(y,x);    inc(d[x]);inc(d[y]);   end;end;procedure dfs(u:longint); var  i:longint;begin  i:=a[u].head;  while i<>0 do   begin    if not vis[i div 2] then     begin      vis[i div 2]:=true;      dfs(a[i].v);      if i div 2<=m then       begin        pr[i div 2,1]:=u;        pr[i div 2,2]:=a[i].v;       end;     end;    i:=a[i].next;  end;end;procedure main; var  i:longint;begin  for i:=1 to n do   if d[i] mod 2<>0 then    begin     adde(i,n+1);     adde(n+1,i);    end   else inc(ans);  writeln(ans);  for i:=1 to n do   dfs(i);  for i:=1 to m do   writeln(pr[i,1],' ',pr[i,2]);end;begin  readln(t);  for i:=1 to t do   begin    init;    main;   end;end.


0 0
原创粉丝点击