O∧O http://acm.hdu.edu.cn/showproblem.php?pid=6074

  2017 Multi-University Training Contest - Team 4 - 1008

  fread真是其乐无穷呀!

  对于每个给出的ai,bi,ci,di,wi,记ai,bi的LCA为pi, 记ci,di的LCA为qi,

  首先根据wi来排序这些电话线

  使用并查集

  首先把ai到pi路径上未加入pi集合的点加入pi集合,然后把bi到pi路径上未加入pi集合的点加入pi集合,

  同样地,把ci到qi路径上未加入qi集合的点加入qi集合,di到qi路径上未加入qi集合的点加入qi集合。

  然后把如果qi未加入pi集合,那么把qi加入pi集合

  然后向上搜未加入集合点的时候,是可以跳跃着做的。

  

  这种代码,写一会想玩会手机。O∧O

624ms代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; namespace fastIO {
#define BUF_SIZE 100000
//fread -> read
bool IOerror = 0;
inline char nc() {
static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
if(p1 == pend) {
p1 = buf;
pend = buf + fread(buf, 1, BUF_SIZE, stdin);
if(pend == p1) {
IOerror = 1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
inline void read(int &x) {
char ch;
while(blank(ch = nc()));
if(IOerror)
return;
for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
}
#undef BUF_SIZE
};
using namespace fastIO; typedef long long ll; const int MAXN = 100010;
const int MAXQ = 200010;//查询数的最大值 //并查集部分
int F[MAXN];//需要初始化为-1
int find(int x)
{
if(F[x] == -1)return x;
return F[x] = find(F[x]);
}
void bing(int u,int v)
{
int t1 = find(u);
int t2 = find(v);
if(t1 != t2)
F[t1] = t2;
}
bool vis[MAXN];//访问标记
int ancestor[MAXN];//祖先 struct Edge
{
int to,next;
}edge[MAXN*2];
int head[MAXN],tot;
void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} struct Query
{
int q,next;
int index;//查询编号
}query[MAXQ*2];
int answer[MAXQ];//存储最后的查询结果,下标0~Q-1
int h[MAXQ];
int tt;
int Q; void add_query(int u,int v,int index)
{
query[tt].q = v;
query[tt].next = h[u];
query[tt].index = index;
h[u] = tt++;
query[tt].q = u;
query[tt].next = h[v];
query[tt].index = index;
h[v] = tt++;
} void init()
{
tot = 0;
memset(head,-1,sizeof(head));
tt = 0;
memset(h,-1,sizeof(h));
memset(vis,false,sizeof(vis));
memset(F,-1,sizeof(F));
memset(ancestor,0,sizeof(ancestor));
} void LCA(int u)
{
ancestor[u] = u;
vis[u] = true;
for(int i = head[u];i != -1;i = edge[i].next)
{
int v = edge[i].to;
if(vis[v])continue;
LCA(v);
bing(u,v);
ancestor[find(u)] = u;
}
for(int i = h[u];i != -1;i = query[i].next)
{
int v = query[i].q;
if(vis[v])
{
answer[query[i].index] = ancestor[find(v)];
}
}
} const int M=100044; struct PhoneLine
{
int a,b,c,d,w,id;
} pl[M]; int n,m;
int fa[M],ansnum,jmp[M],num[M];
int stk[M],lstk,dep[M];
ll cst[M],anscst; bool cmp(PhoneLine x,PhoneLine y)
{
return x.w<y.w;
} void getjmp(int rt,int pa,int depth)
{
dep[rt]=depth;
int i,j,v;
jmp[rt]=pa;
for(i=head[rt];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(v==pa) continue;
getjmp(v,rt,depth+1);
}
} void init2()
{
int i,j;
for(i=1;i<=n;i++)
{
fa[i]=i;
num[i]=1;
cst[i]=0;
}
ansnum=0; anscst=0;
getjmp(1,0,1);
} int fff(int x)
{
if(fa[x]==x)
return x;
fa[x]=fff(fa[x]);
return fa[x];
} void merge(int a,int t,int w)
{
int i,j,pa,pt,tmp;
while(dep[a]>dep[t])
{
tmp=jmp[a];
jmp[a]=jmp[t];
pa=fff(a); pt=fff(t);
if(pa!=pt)
{
fa[pa]=pt;
num[pt]+=num[pa];
cst[pt]+=cst[pa]+w;
}
a=tmp;
}
} void solve()
{
int i,j,a,b,w,pa,pb,t,tmp;
for(i=0;i<m;i++)
{
w=pl[i].w;
t=answer[i*2];
a=pl[i].a; b=pl[i].b;
merge(a,t,w);
merge(b,t,w);
t=answer[i*2+1];
a=pl[i].c; b=pl[i].d;
merge(a,t,w);
merge(b,t,w);
a=answer[i*2]; b=answer[i*2+1];
pa=fff(a); pb=fff(b);
if(pa!=pb)
{
fa[pa]=pb;
num[pb]+=num[pa];
cst[pb]+=cst[pa]+w;
}
}
} int main()
{
// freopen("数据\\1008.in","r",stdin);
// freopen("数据\\1008my.out","w",stdout);
int i,j,u,v;
int cas;
read(cas);
while(cas--)
{
init();
read(n); read(m);
for(i=1;i<n;i++)
{
read(u); read(v);
addedge(u,v);
addedge(v,u);
}
for(i=0;i<m;i++)
{
pl[i].id=i;
read(pl[i].a); read(pl[i].b); read(pl[i].c); read(pl[i].d); read(pl[i].w);
}
sort(pl,pl+m,cmp);
Q=2*m;
for(i=0;i<m;i++)
{
add_query(pl[i].a,pl[i].b,i*2);
add_query(pl[i].c,pl[i].d,i*2+1);
}
LCA(1);
// printf("LCA finished\n");
init2();
solve();
for(i=1;i<=n;i++)
if(num[i]>ansnum)
{
ansnum=num[i];
anscst=cst[i];
}
printf("%d %lld\n",ansnum,anscst);
}
return 0;
}

  

hdu 6074 Phone Call的更多相关文章

  1. HDU 6074 - Phone Call | 2017 Multi-University Training Contest 4

    看标程的代码这么短,看我的.... 难道是静态LCA模板太长了? /* HDU 6074 - Phone Call [ LCA,并查集 ] | 2017 Multi-University Traini ...

  2. HDU 6074 Phone Call LCA + 并查集

    Phone Call Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Pro ...

  3. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  5. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  6. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  8. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  9. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. 【php设计模式】单例模式

    实现单例的三个关键点: 1.使用一个静态成员来保持一个单例实例 2.一个私有的构造方法使得该类只能在类的内部方法中被实例化 3.在实例化对象的静态方法中,先判断静态变量是否已经被赋值,如果赋值则返回该 ...

  2. python 之 Urllib库的基本使用

    目录 python 之 Urllib库的基本使用 官方文档 什么是Urllib urlopen url参数的使用 data参数的使用 timeout参数的使用 响应 响应类型.状态码.响应头 requ ...

  3. django类视图的使用

    1 类视图引入 以函数的方式定义的视图称为函数视图,函数视图便于理解. 但是遇到一个视图对应的路径提供了多种不同HTTP请求方式的支持时,便需要在一个函数中编写不同的业务逻辑,代码可读性与复用性都不佳 ...

  4. spring-boot-plus集成Shiro+JWT权限管理

    SpringBoot+Shiro+JWT权限管理 Shiro Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理. 使用Shiro的易于理解的API,您可以 ...

  5. JSP读取properties文件变量

    1.jsp代码 <%ResourceBundle res = ResourceBundle.getBundle("properties文件名"); %> 2.js代码 ...

  6. ASP .NET依赖注入理解

    ASP .NET依赖注入理解[转]:  https://www.cnblogs.com/wzk153/p/10892444.html

  7. Django rest-framework框架-CBV原理

    jdango中间件:class Test(View): def dispatch(self, request, *args, **kwargs): #第一种方法 func = getattr(self ...

  8. 忘记root密码,修改方法

    Linux的root密码修改不像Windows的密码修改找回,Windows的登录密码忘记需要介入工具进行解决.CentOS6和CentOS7的密码方法也是不一样的,具体如下: 首先是CentOS 6 ...

  9. Image Processing and Computer Vision_Review:A survey of recent advances in visual feature detection—2014.08

    翻译 一项关于视觉特征检测的最新进展概述——http://tongtianta.site/paper/56761 摘要 -特征检测是计算机视觉和图像处理中的基础和重要问题.这是一个低级处理步骤,它是基 ...

  10. 2.Bacula Server端安装配置

    1.  Bacula Server端安装配置 1.1.  Bacula Server端安装 1.1.1.  安装bacula依赖包 For Centos6: yum install -y mysql ...