日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > > 充電吧
[導(dǎo)讀]題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 題面: Ponds Time Limit: 1500/1000 MS (Java/Other

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5438


題面:

Ponds Time Limit: 1500/1000 MS (Java/Others)????Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1308????Accepted Submission(s): 439


Problem Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and there will not be more than one pipe between two ponds. Each pond has a value v.

Now Betty wants to remove some ponds because she does not have enough money. But each time when she removes a pond, she can only remove the ponds which are connected with less than two ponds, or the pond will explode.

Note that Betty should keep removing ponds until no more ponds can be removed. After that, please help her calculate the sum of the value for each connected component consisting of a odd number of ponds ?
Input The first line of input will contain a number T(1≤T≤30) which is the number of test cases.

For each test case, the first line contains two number separated by a blank. One is the number p(1≤p≤104) which represents the number of ponds she owns, and the other is the number m(1≤m≤105) which represents the number of pipes.

The next line contains p numbers v1,...,vp, where vi(1≤vi≤108) indicating the value of pond i.

Each of the last m lines contain two numbers a and b, which indicates that pond a and pond b are connected by a pipe. ?
Output For each test case, output the sum of the value of all connected components consisting of odd number of ponds after removing all the ponds connected with less than two pipes. ?
Sample Input
1
7 7
1 2 3 4 5 6 7
1 4
1 5
4 5
2 3
2 6
3 6
2 7

?

Sample Output
21

?

Source 2015 ACM/ICPC Asia Regional Changchun Online


題目大意:

??? 給定一些池塘,池塘之間連有一些管道。要求將不連管道或者只連一根管道的池塘消去,問(wèn)最后剩下連在一起且池塘個(gè)數(shù)為奇數(shù)的池塘權(quán)值總和。


解題:

??? 比賽的時(shí)候,以為是什么奇環(huán),后來(lái)發(fā)現(xiàn)又不是,思路整體跑偏了。正確的解法是,通過(guò)bfs或者dfs不斷消去度數(shù)為1的點(diǎn),消完之后,再用dfs找聯(lián)通塊即可。以下提供兩種解法,實(shí)際上沒(méi)什么實(shí)質(zhì)差別,復(fù)雜度都為O(m),因?yàn)槊織l邊最多遍歷一次。


解法一:

??? dfs+dfs

代碼:

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define LL long long
int pond[10010];
//鄰接表
struct edge
{
	int next,to,vis;//下一條邊序號(hào),到哪個(gè)點(diǎn),邊是否被斷開(kāi)
}store[200010];
int head[10010],cnt,degree[10010],num;//head存儲(chǔ)每個(gè)點(diǎn)連向的第一條邊的存儲(chǔ)下標(biāo),-1代表該點(diǎn)當(dāng)前已經(jīng)沒(méi)有邊了
//cnt加邊的下標(biāo),degree度數(shù),num聯(lián)通塊中的塊數(shù)
bool vist[10010];//數(shù)聯(lián)通塊時(shí)的標(biāo)記
LL sum;//存儲(chǔ)聯(lián)通塊的總和
//加邊
void addedge(int a,int b)
{
   store[cnt].to=b;
   store[cnt].next=head[a];
   store[cnt].vis=false;
   head[a]=cnt++;
}
//消去池塘
void dfs(int x)
{
   int tmp;
   tmp=head[x];
   degree[x]=0;
   //還未到最后一條邊
   while(tmp!=-1)
   {
	   if(degree[store[tmp].to])
	   {
		  //tmp和tmp^1代表相鄰兩條邊
          store[tmp].vis=store[tmp^1].vis=true;
          degree[store[tmp].to]--;
		  //如果度數(shù)為1,繼續(xù)搜索
		  if(degree[store[tmp].to]==1)
		  	  dfs(store[tmp].to);
	   }
	   //移向下一條邊
	   tmp=store[tmp].next;
   }
   return;
}
void dfs2(int x)
{
   int tmp,v;
   vist[x]=1;
   tmp=head[x];
   while(tmp!=-1)
   {
	 //如果這條邊還沒(méi)斷開(kāi)
     if(!store[tmp].vis)
     {
         v=store[tmp].to;
         if(!vist[v]&°ree[v])
         {
             num++;
             sum+=pond[v];
             dfs2(v);
         }
     }
     tmp=store[tmp].next;
   }
}
int main()
{
    int t,p,m,u,v,tmp,cas=1;
	LL ans;
	scanf("%d",&t);
	while(t--)
	{
      scanf("%d%d",&p,&m);
	  memset(head,-1,sizeof(head));
	  memset(degree,0,sizeof(degree));
	  memset(vist,0,sizeof(vist));
	  cnt=ans=0;
      for(int i=1;i<=p;i++)
		  scanf("%d",&pond[i]);
	  for(int i=1;i<=m;i++)
	  {
		  scanf("%d%d",&u,&v);
		  addedge(u,v);
		  addedge(v,u);
          degree[u]++;
		  degree[v]++;
	  }
	  //搜索度數(shù)為1的池塘
	  for(int i=1;i<=p;i++)
		  if(degree[i]==1)
			  dfs(i);
	  //統(tǒng)計(jì)結(jié)果
	 for(int i=1;i<=p;i++)
      {
          if(degree[i]&&!vist[i])
          {
              sum=pond[i];
              num=1;
              dfs2(i);
              if(num%2)
                  ans+=sum;
          }
      }
      printf("%lldn",ans);
	}
	return 0;
}



解法二:

??? bfs+dfs

代碼:

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int pond[10010];
struct edge
{
    int next,to;
}store[200010];
int head[10010],cnt,degree[10010];
bool vist[10010],cut_off[200010];
long long sum;
int num;
queue  qe;
inline void addedge(int a,int b)
{
   store[cnt].to=b;
   store[cnt].next=head[a];
   head[a]=cnt++;
}
void bfs()
{
    int tmp,cur,v;
    while(!qe.empty())
    {
      cur=qe.front();
      qe.pop();
      tmp=head[cur];
      while(~tmp)
      {
          v=store[tmp].to;
          if(degree[v])
          {
              cut_off[tmp]=cut_off[tmp^1]=1;
              degree[v]--;
              if(degree[v]==1)
              {
                  qe.push(v);
                  degree[v]=0;
              }
          }
          tmp=store[tmp].next;
      }
    }
}
void dfs(int x)
{
   int tmp,v;
   vist[x]=1;
   tmp=head[x];
   while(tmp!=-1)
   {
     if(!cut_off[tmp])
     {
         v=store[tmp].to;
         if(!vist[v])
         {
             num++;
             sum+=pond[v];
             dfs(v);
         }
     }
     tmp=store[tmp].next;
   }
}
int main()
{
    int t,p,m,u,v;
    long long  ans;
    scanf("%d",&t);
    while(t--)
    {
      scanf("%d%d",&p,&m);
      memset(head,-1,sizeof(head));
      memset(degree,0,sizeof(degree));
      memset(vist,0,sizeof(vist));
      memset(cut_off,0,sizeof(cut_off));
      cnt=0,ans=0;
      for(int i=1;i<=p;i++)
          scanf("%d",&pond[i]);
      for(int i=1;i<=m;i++)
      {
          scanf("%d%d",&u,&v);
          addedge(u,v);
          addedge(v,u);
          degree[u]++;
          degree[v]++;
      }
      for(int i=1;i<=p;i++)
          if(degree[i]==1)
               qe.push(i),degree[i]=0;
      bfs();
      for(int i=1;i<=p;i++)
      {
          if(degree[i]&&!vist[i])
          {
              sum=pond[i];
              num=1;
              dfs(i);
              if(num%2)
                  ans+=sum;
          }
      }
      printf("%lldn",ans);
    }
    return 0;
}


本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

LED驅(qū)動(dòng)電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: 驅(qū)動(dòng)電源

在工業(yè)自動(dòng)化蓬勃發(fā)展的當(dāng)下,工業(yè)電機(jī)作為核心動(dòng)力設(shè)備,其驅(qū)動(dòng)電源的性能直接關(guān)系到整個(gè)系統(tǒng)的穩(wěn)定性和可靠性。其中,反電動(dòng)勢(shì)抑制與過(guò)流保護(hù)是驅(qū)動(dòng)電源設(shè)計(jì)中至關(guān)重要的兩個(gè)環(huán)節(jié),集成化方案的設(shè)計(jì)成為提升電機(jī)驅(qū)動(dòng)性能的關(guān)鍵。

關(guān)鍵字: 工業(yè)電機(jī) 驅(qū)動(dòng)電源

LED 驅(qū)動(dòng)電源作為 LED 照明系統(tǒng)的 “心臟”,其穩(wěn)定性直接決定了整個(gè)照明設(shè)備的使用壽命。然而,在實(shí)際應(yīng)用中,LED 驅(qū)動(dòng)電源易損壞的問(wèn)題卻十分常見(jiàn),不僅增加了維護(hù)成本,還影響了用戶體驗(yàn)。要解決這一問(wèn)題,需從設(shè)計(jì)、生...

關(guān)鍵字: 驅(qū)動(dòng)電源 照明系統(tǒng) 散熱

根據(jù)LED驅(qū)動(dòng)電源的公式,電感內(nèi)電流波動(dòng)大小和電感值成反比,輸出紋波和輸出電容值成反比。所以加大電感值和輸出電容值可以減小紋波。

關(guān)鍵字: LED 設(shè)計(jì) 驅(qū)動(dòng)電源

電動(dòng)汽車(EV)作為新能源汽車的重要代表,正逐漸成為全球汽車產(chǎn)業(yè)的重要發(fā)展方向。電動(dòng)汽車的核心技術(shù)之一是電機(jī)驅(qū)動(dòng)控制系統(tǒng),而絕緣柵雙極型晶體管(IGBT)作為電機(jī)驅(qū)動(dòng)系統(tǒng)中的關(guān)鍵元件,其性能直接影響到電動(dòng)汽車的動(dòng)力性能和...

關(guān)鍵字: 電動(dòng)汽車 新能源 驅(qū)動(dòng)電源

在現(xiàn)代城市建設(shè)中,街道及停車場(chǎng)照明作為基礎(chǔ)設(shè)施的重要組成部分,其質(zhì)量和效率直接關(guān)系到城市的公共安全、居民生活質(zhì)量和能源利用效率。隨著科技的進(jìn)步,高亮度白光發(fā)光二極管(LED)因其獨(dú)特的優(yōu)勢(shì)逐漸取代傳統(tǒng)光源,成為大功率區(qū)域...

關(guān)鍵字: 發(fā)光二極管 驅(qū)動(dòng)電源 LED

LED通用照明設(shè)計(jì)工程師會(huì)遇到許多挑戰(zhàn),如功率密度、功率因數(shù)校正(PFC)、空間受限和可靠性等。

關(guān)鍵字: LED 驅(qū)動(dòng)電源 功率因數(shù)校正

在LED照明技術(shù)日益普及的今天,LED驅(qū)動(dòng)電源的電磁干擾(EMI)問(wèn)題成為了一個(gè)不可忽視的挑戰(zhàn)。電磁干擾不僅會(huì)影響LED燈具的正常工作,還可能對(duì)周圍電子設(shè)備造成不利影響,甚至引發(fā)系統(tǒng)故障。因此,采取有效的硬件措施來(lái)解決L...

關(guān)鍵字: LED照明技術(shù) 電磁干擾 驅(qū)動(dòng)電源

開(kāi)關(guān)電源具有效率高的特性,而且開(kāi)關(guān)電源的變壓器體積比串聯(lián)穩(wěn)壓型電源的要小得多,電源電路比較整潔,整機(jī)重量也有所下降,所以,現(xiàn)在的LED驅(qū)動(dòng)電源

關(guān)鍵字: LED 驅(qū)動(dòng)電源 開(kāi)關(guān)電源

LED驅(qū)動(dòng)電源是把電源供應(yīng)轉(zhuǎn)換為特定的電壓電流以驅(qū)動(dòng)LED發(fā)光的電壓轉(zhuǎn)換器,通常情況下:LED驅(qū)動(dòng)電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: LED 隧道燈 驅(qū)動(dòng)電源
關(guān)閉