日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

NOIP 2014 联合权值

發(fā)布時(shí)間:2024/10/14 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NOIP 2014 联合权值 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接:

https://www.luogu.org/problemnew/show/P1351

參考洛谷題解:

使用鏈?zhǔn)角跋蛐莾?chǔ)存圖。如果使用深度優(yōu)先搜索的話,是會(huì)超時(shí)的,如果遍歷中間的點(diǎn),雖然比直接遍歷快一點(diǎn),但是也會(huì)超時(shí),畫一個(gè)有三個(gè)頂點(diǎn)一個(gè)中心的菊花圖,題目就是要求三個(gè)頂點(diǎn)兩兩相乘的和,找規(guī)律可得到:結(jié)果為三個(gè)頂點(diǎn)的和的平方減去三個(gè)頂點(diǎn)的平方和,推廣到n個(gè)頂點(diǎn)也是一樣的。由此得出代碼:

#include <iostream> #include <cstring> #include <stdio.h> using namespace std; typedef long long ll; const int inf=1e6+7; struct node {int from,to,next; }themap[inf]; int head[inf],thevalue[inf],thenow[inf]; int cnt=0; int themax=-1,theans=0;void addedge(int u,int v) //遠(yuǎn)不如直接在用一個(gè)來(lái)的好。 {themap[cnt].from=u;themap[cnt].to=v;themap[cnt].next=head[u];head[u]=cnt++;themap[cnt].from=v;themap[cnt].to=u;themap[cnt].next=head[v];head[v]=cnt++; }int main() {int n,u,v;memset(head,-1,sizeof(head));scanf("%d",&n);for(int i=1;i<n;i++){scanf("%d %d",&u,&v);addedge(u,v);}for(int i=1;i<=n;i++)scanf("%d",&thevalue[i]);for(int i=1;i<=n;i++){int flag=0;ll one=-1,two=-1; //最大值和次大值。ll temp1=0;ll temp2=0;for(int j=head[i];j!=-1;j=themap[j].next){//thenow[flag++]=themap[j].to;int now=themap[j].to; //是其中的值啊.if(thevalue[now]>one){two=one;one=thevalue[now];}else if(thevalue[now]>two)two=thevalue[now];temp1+=thevalue[now];temp1=temp1%10007;temp2+=(thevalue[now]*thevalue[now])%10007;temp2=temp2%10007; flag++;}if(flag==1){}else{if(one*two>themax){themax=one*two; }temp1=(temp1*temp1)%10007;theans+=(temp1-temp2+10007); //防止theans成為負(fù)的。theans=theans%10007; }}printf("%d %d\n",themax,theans%10007);return 0; }

?

與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的NOIP 2014 联合权值的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。