BZOJ2657 [Zjoi2012] Travel (journey) [The diameter of the tree]

topic

 到了难得的暑假,为了庆祝小白在数学考试中取得的优异成绩,小蓝决定带小白出去旅游~~

经过一番抉择,两人决定将T国作为他们的目的地。T国的国土可以用一个凸N边形来表示,N个顶点表示N个入境/出境口。T国包含N-2个城市,每个城市都是顶点均为N边形顶点的三角形(换而言之,城市组成了关于T国的一个三角剖分)。两人的旅游路线可以看做是连接N个顶点中不相邻两点的线段。


In order to be able to buy the best souvenirs, Xiaobai hopes to pass through as many cities as possible on the tourist route. As a friend of Xiaolan, can you help Xiaolan?

input format

Only one test data is included in each input file.
The first line contains two positive integers N separated by spaces. The meaning of N is as described in the title.
Next there are N-2 lines, each line contains three integers p, q, r, representing the numbers of the three vertices of the city triangle (the N vertices of country T are numbered chronologically from 1 to n).

output format

  输出文件共包含1行,表示最多经过的城市数目。(一个城市被当做经过当且仅当其与线路有至少两个公共点)

input sample

  6

 1 2 4

 2 3 4

 1 4 5

 1 5 6

Sample output

4

hint

4<=N<=200000

answer

Question: Find a diagonal line that passes through as many triangles as possible

The plane graph is associated with the dual graph, and it is found that the dual graph is a tree, and the answer is the diameter of the tree

When building a map, put the three sides of each triangle row together and sort, there are at most two identical sides, and these two are adjacent triangles

#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 200005,maxm = 100005,INF = 1000000000;
inline int read(){
    int out = 0,flag = 1; char c = getchar();
    while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    return out * flag;
}
int h[maxn],ne = 2;
struct EDGE{int to,nxt;}ed[maxn << 1];
inline void build(int u,int v){
    ed[ne] = (EDGE){v,h[u]}; h[u] = ne++;
    ed[ne] = (EDGE){u,h[v]}; h[v] = ne++;
}
struct line{
    int a,b,id;
}e[maxn * 3];
inline bool operator < (const line& a,const line& b){
    return a.a == b.a ? a.b < b.b : a.a < b.a;
}
int n,m,vis[maxn],d[maxn],q[maxn];
void order(int& a,int& b,int& c){
    if (a > b) swap(a,b);
    if (a > c) swap(a,c);
    if (b > c) swap(b,c);
}
int head,tail;
void solve(){
    q[head = tail = 1] = 1;
    int u,rt = 1; vis[1] = true;
    while (head <= tail){
        u = q[head++];
        if (d[u] > d[rt]) rt = u;
        Redge(u) if (!vis[to = ed[k].to]){
            d[to] = d[u] + 1; vis[to] = true;
            q[++tail] = to;
        }
    }
    memset(d,0,sizeof(d));
    memset(vis,0,sizeof(vis));
    q[head = tail = 1] = rt; d[rt] = 1; vis[rt] = true;
    while (head <= tail){
        u = q[head++];
        if (d[u] > d[rt]) rt = u;
        Redge(u) if (!vis[to = ed[k].to]){
            d[to] = d[u] + 1; vis[to] = true;
            q[++tail] = to;
        }
    }
    printf("%d\n",d[rt]);
}
int main(){
    n = read();
    int a,b,c;
    REP(i,n - 2){
        a = read(); b = read(); c = read();
        order(a,b,c);
        e[++m] = (line){a,b,i};
        e[++m] = (line){b,c,i};
        e[++m] = (line){a,c,i};
    }
    sort(e + 1,e + 1 + m);
    for (int i = 1; i <= m; i++){
        if (e[i].a == e[i - 1].a && e[i].b == e[i - 1].b)
            build(e[i].id,e[i - 1].id);
    }
    solve();
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324974060&siteId=291194637