ZOJ Problem Set,zojproblemset

來源:互聯網
上載者:User

ZOJ Problem Set,zojproblemset

題目:ZOJ Problem Set - 3820 Building Fire Stations 


題意:給出n個點,n-1條邊的一棵樹,然後要在兩個點上建立兩個消防站,讓所有點的到消防站最大距離的點的這個距離最小。


分析:首先先求這個樹的直徑,然後在樹的直徑的中點處把樹分成兩棵樹,然後在把兩棵樹分別取中點的最大值就是ans值。

這個題目資料有點水了感覺。。。

AC代碼:


#include <cstdio>#include <iostream>#include <algorithm>#include <vector>#include <cstring>#include <queue>using namespace std;const int N = 204000;int n;vector<int> v[N];int vis[N],father[N];vector<int> line;int BFS(int s,int flag){    queue<int> q;    int e=s;    line.clear();    memset(vis,0,sizeof(vis));    memset(father,-1,sizeof(father));    vis[flag]=1;    q.push(s);    vis[s]=1;    int ans=1;    while(!q.empty())    {        int f=q.front();        q.pop();        for(int i=0; i<v[f].size(); i++){            int k=v[f][i];            if(vis[k])                continue;            vis[k]=vis[f]+1;            father[k]=f;            if(vis[k]>ans){                e=k;                ans=vis[k];            }            q.push(k);        }    }    for(int i=e;i!=-1;i=father[i])        line.push_back(i);    return e;}struct Node{    int one,two,ans;};void print()  //輸出{    for(int i = 0;i<line.size();i++)    {        printf("x%d ",line[i]);    }    printf("\n");}pair<int,int> Yougth(int s,int t){    int p1 = BFS(s,t);    int p2 = BFS(p1,t);    int len = line.size();    int one = line[len/2];    int tmp = len/2;    pair<int,int> ans(one,tmp);    return ans;}Node Importent(int fir,int sec){    Node pps;    int ans = -1;    pair<int,int> tt = Yougth(fir,sec);    pps.one = tt.first;    ans = max(ans,tt.second);    tt = Yougth(sec,fir);    pps.two = tt.first;    ans = max(ans , tt.second);    pps.ans = ans;    return pps;}void solve(){    Node pps,ans2;    int p1 = BFS(1,n+1);    int p2 = BFS(p1,n+1);    int len = line.size()/2;    int a = line[len-1],b = line[len] , c = line[len+1];    if(line.size()%2==0)    {        pps = Importent(a,b);    }    else    {        pps = Importent(a,b);        ans2 = Importent(b,c);        if(ans2.ans<pps.ans)    //取小的。但是好像沒有這樣的資料            pps = ans2;    }    printf("%d %d %d\n",pps.ans,pps.one,pps.two);}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(int i=0;i<n-1;i++)        {            int x,y;            scanf("%d%d",&x,&y);            v[x].push_back(y);            v[y].push_back(x);        }        solve();        for(int i=0;i<=n;i++)            v[i].clear();    }    return 0;}



ZOJ Problem Set

1.你的代碼很亂
2 你沒有說是哪道題
 
ZOJ Problem Set - 1001:A + B Problem的如下解答為何報錯?

#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
printf("%d\n",a+b);
return 0;
}

主程式應返回int。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.