Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

一个O(m)的算法

Posted by xiaoMM at 2008-09-29 21:31:25 and last updated at 2008-09-29 21:32:23
In Reply To:求教合肥赛第八题怎么做啊? Posted by:doommetal at 2008-09-29 19:53:12
只考虑破坏图,进行染色。
白色表示在原图中与1相连,否则为黑色

初始化:
将破坏图中未与1连接的点还有1染成白色,其他点为无色,同时记录此时白色点数量为total。

外层循环遍历,对未染色的点,DFS进行染色。

for(int i = 0; i < n; i++)
  if(!color[i])
    DFS(i)

DFS函数.
void DFS(int k){
  visit[k] = 1;
  int cnt = 0;
  for(i = 1~size)//邻接点{
    if(!visit[i]) DFS(i);
    if(color[i] == WHITE) cnt++;
  }
  if(cnt == total)
    color[k] = BLACK;
  else{
    color[k] = WHITE;
    total++;
  }
}

最后节点数为total - 1(减去第一个点)

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator