/ Randle /

记录详情

Accepted


  
# 状态 耗时 内存占用
#1 Accepted 4ms 2.348 MiB
#2 Accepted 4ms 2.25 MiB
#3 Accepted 4ms 2.375 MiB
#4 Accepted 4ms 2.25 MiB
#5 Accepted 215ms 2.375 MiB
#6 Accepted 164ms 2.473 MiB
#7 Accepted 157ms 744.0 KiB
#8 Accepted 199ms 2.375 MiB
#9 Accepted 137ms 2.602 MiB
#10 Accepted 214ms 2.496 MiB
#11 Accepted 213ms 3.25 MiB
#12 Accepted 226ms 3.805 MiB
#13 Accepted 243ms 2.891 MiB
#14 Accepted 312ms 2.875 MiB
#15 Accepted 240ms 3.875 MiB
#16 Accepted 244ms 2.355 MiB
#17 Accepted 721ms 3.82 MiB
#18 Accepted 741ms 3.016 MiB
#19 Accepted 546ms 3.434 MiB
#20 Accepted 1911ms 3.734 MiB

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read_int()
{
	char ch=getchar();
	int x=0;
	while(!isdigit(ch))
		ch=getchar();
	while(isdigit(ch))
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x;
}
inline ll read_ll()
{
	char ch=getchar();
	ll x=0;
	while(!isdigit(ch))
		ch=getchar();
	while(isdigit(ch))
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x;
}
int n,m,q;
struct edge1
{
    int x,y;
    ll l;
}e1[100005],e2[10005];
int cnt1=0,cnt2=0;

struct edge
{
	int to,nex;
	ll l;
}e[10005];
int point[5005],cnt=0;

int father[5005],deep[5005];
int fa[10005][16];
ll dis[10005][16];
inline void add1(int x,int y,ll l)
{
    e1[++cnt1].x=x;
    e1[cnt1].y=y;
    e1[cnt1].l=l;
}
inline void add2(int x,int y,ll l)
{
	e2[++cnt2].x=x;
    e2[cnt2].y=y;
    e2[cnt2].l=l;
}
inline void add(int x,int y,ll l)
{
    e[++cnt].to=y;
    e[cnt].nex=point[x];
    point[x]=cnt;
    e[cnt].l=l;
}
inline int find(int x)
{
    if(father[x]!=x) father[x]=find(father[x]);
    return father[x];
}
inline bool cmp(edge1 x,edge1 y)
{
	return x.l<y.l;
}
void Kruskal1()
{
	int i;
	sort(e1+1,e1+cnt1+1,cmp);
    for(i=1;i<=n;i++)
		father[i]=i;
    for(i=1;i<=m;i++)
    {
        int f1=find(e1[i].x);
		int f2=find(e1[i].y);
        if(f1!=f2)
		{
			add(e1[i].x,e1[i].y,e1[i].l);
			add(e1[i].y,e1[i].x,e1[i].l);
			add2(e1[i].x,e1[i].y,e1[i].l);
			father[f2]=f1;
		}
    }
}
void Kruskal2()
{
	int i,len=cnt2;
	sort(e2+1,e2+cnt2+1,cmp);
    for(i=1;i<=n;i++)
		father[i]=i;
	cnt2=0;
    for(i=1;i<=len;i++)
    {
        int f1=find(e2[i].x);
		int f2=find(e2[i].y);
        if(f1!=f2)
		{
			add(e2[i].x,e2[i].y,e2[i].l);
			add(e2[i].y,e2[i].x,e2[i].l);
			add2(e2[i].x,e2[i].y,e2[i].l);
			father[f2]=f1;
		}
    }
}
void dfs(int u)
{
	int i;
    for(i=point[u];i;i=e[i].nex)
        if(fa[u][0]!=e[i].to)
        {
        	fa[e[i].to][0]=u;
        	dis[e[i].to][0]=e[i].l;
        	deep[e[i].to]=deep[u]+1;
        	dfs(e[i].to);
		}
}
inline ll maxx(ll x,ll y)
{
	if(x>y) return x;
	else return y;
}
inline void init()
{
    for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i<=n;i++)
        {
        	fa[i][j]=fa[fa[i][j-1]][j-1];
        	dis[i][j]=maxx(dis[i][j-1],dis[fa[i][j-1]][j-1]);
		}
            
}
ll get_lca_and_max(int x,int y)
{
	int i;
	ll ret=0;
    if(deep[x]>deep[y]) swap(x,y);
    int f=deep[y]-deep[x];
    for(i=0;(1<<i)<=f;i++)
        if(f&(1<<i))
            ret=maxx(ret,dis[y][i]),y=fa[y][i];
    if(x!=y)
    {
        for(i=(int)log2(n);i>=0;i--)
        {
            if(fa[x][i]!=fa[y][i])
            {
            	ret=maxx(ret,dis[x][i]);
            	ret=maxx(ret,dis[y][i]);
                x=fa[x][i];
                y=fa[y][i];
            }
        }
        ret=maxx(ret,dis[x][0]);
        ret=maxx(ret,dis[y][0]);
    }
    return ret;
}

int main()
{
	//freopen("game1.in","r",stdin);
	//freopen("game.out","w",stdout);
	int i,x,y,m1,m2,b1,b2;
	string s;
	long long z;
    n=read_int(),m=read_int();
    for(i=1;i<=m;i++)
    {
        x=read_int(),y=read_int(),z=read_ll();
        add1(x,y,z);
    }
    Kruskal1();
   	dfs(1);
   	init();
    q=read_int();
    while(q--)
    {
    	cin>>s;
    	if(s=="game")
    	{
    		m1=read_int(),m2=read_int();
    		b1=read_int(),b2=read_int();
 			if(get_lca_and_max(m1,m2)==get_lca_and_max(b1,b2))
				printf("Baozika\n");
			else
				printf("madoka\n");	
		}
		else
		{
			x=read_int(),y=read_int(),z=read_ll();
			if(z<e2[cnt2].l)
			{
				memset(point,0,sizeof(point));
				cnt=0;
				add2(x,y,z);
				Kruskal2();
				dfs(1);
				init();
			}
		}
    }
    return 0;
}

信息

递交者
类型
递交
题目
玩游戏(CQ直辖市noip模拟赛联考) T2
题目数据
下载
语言
C++
递交时间
2017-11-04 20:10:54
评测时间
2017-11-04 20:10:54
评测机
分数
100
总耗时
6509ms
峰值内存
3.875 MiB