被卡常了求助!orz求求大佬帮我解决
  • 板块灌水区
  • 楼主K1ondra
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/6/20 21:38
  • 上次更新2024/6/21 11:01:13
查看原帖
被卡常了求助!orz求求大佬帮我解决
586689
K1ondra楼主2024/6/20 21:38
#include<bits/stdc++.h>

using namespace std;
const int N=6010;

int fa[N];
struct node {
	int u,v,w;
} E[N*2];
int n,m;

inline int read() {
	int x=0;
	char c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
	return x;
}

int find(int& x) {
	return fa[x]==x?x:fa[x]=find(fa[x]);
}

bool cmp(node a,node b) {
	return a.w<b.w;
}

int main() {
  ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	n=read();
	m=read();

	for(register int i=1; i<=m; i++) {
		E[i].u=read();
		E[i].v=read();
		E[i].w=read();
		if(i<n-1) {
			cout<<-1<<"\n"; 
			continue;
		} 
    else {
			int tot=0;
			int answer=0;
			int flag=1;
			sort(E+1,E+i+1,cmp);
			for(register int j=1; j<=n; j++)
				fa[j]=j;
			for(register int j=1; j<=i&&flag; j++) {
				int fu=find(E[j].u);
				int fv=find(E[j].v);
				if(fu!=fv) {
					fa[fu]=fv;
					tot++,answer+=E[j].w;
				}
				// if(tot==n-1)
				// 	flag=0;
        (tot==n-1)&&(flag=0);
			}
			// if(tot!=n-1)
			// 	cout<<-1<<"\n";
			// else
			// 	cout<<answer<<"\n";
      (tot!=n-1)?(cout<<-1<<"\n"):(cout<<answer<<"\n");
		}
	}
	return 0;
}
/* 
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
                                                                                                                          ||                       |||                                                                                                    
                       |||                      |||                   ||    ||                           ||               |||                       ||| |||                                                                                               
                        ||   ||                  ||   ||              |||   |||                          |||               ||                   ||  ||  ||||                      |||                                                                     
                        ||   ||||                ||   ||||            |||  |||                   ||       ||               || ||||              ||| ||||||              ||||||||||||||           |||||||       ||||||||||||||||||||||      |||||||        
                        ||    |||                ||    |||           |||   |||              |||||||||     ||               |||||||               || ||    ||||          ||||||||  |||||        |||||  ||||     ||||||||||||||||||||||    |||||  ||||      
                        |||||| |                 |||||| |            |||   ||    |           ||   ||| |   ||               ||||              |||||||||||||||||||          ||      ||||        ||||      |||     |||    |||  ||    ||    ||||      |||     
                      ||||||||                 ||||||||             |||   ||| ||||||         ||   ||  ||  ||               ||                ||||||||||||  |||||                 |||         ||||        |||     |||   |||  ||    |    ||||        |||    
                   ||||||||| ||             ||||||||| ||            |||   ||||||||||          |||||   ||  ||               ||   ||||||       || |||||||||| |||                ||||           |||         |||     |||    ||| |     |    |||         |||    
                    ||  ||   |||             ||  ||   |||          ||||  |||||  |||           |||||   ||  ||       ||||||||||||||||||||     |||  ||||  ||                     |||           ||||          |||     |||   |||||    ||   ||||          |||   
                        ||  ||||                 ||  ||||         |||||  || ||  ||             ||     ||  ||      |||||||| ||               ||   ||  |||                      |||           |||           |||     |||   ||||     |    |||           |||   
                  ||||  ||||||             ||||  ||||||          ||| || ||  ||| |              |||||  ||  ||        |      ||                     |||||                        ||           |||           |||     |||    |||    ||    |||           |||   
                   |||  ||||                |||  ||||            ||  ||      ||            |||||||||  ||  ||               || |                   | || |||                     ||           |||           |||      |||   |||    ||    |||           |||   
                      |||||                    |||||                 ||  |   || |          |||||||||  ||  ||               |||||||              |||||||||||                    ||           |||           |||      |||   ||||   |     |||           |||   
                    ||| |||||                ||| |||||               || ||   || |||           || |||  ||  ||               || |||||             ||||||   ||                    ||           ||||          |||       |||  ||||  ||     ||||          |||   
                  ||||  || |||             ||||  || |||              || ||   || ||||          || |||  ||  ||               ||   |||             ||  ||   ||                    ||           ||||          ||        ||| || ||| |      ||||          ||    
                |||||   ||  |||||        |||||   ||  |||||           |||||   ||  |||         ||  |||      ||               ||                   ||  ||   ||                    ||            |||         |||        ||| || |||||       |||         |||    
               ||||     ||   ||||||||   ||||     ||   ||||||||      ||||||   ||   |||       |||||||       ||               ||                   ||  || ||||                    ||             |||       |||          ||||   ||||        |||       |||     
                ||      ||    |||||||    ||      ||    |||||||      |||||  | ||    ||      ||| ||||       ||               ||                   ||  ||  |||                 |  ||              ||||    |||           ||||   |||          ||||    |||      
                     || ||      ||            || ||      ||         |||    ||||           |||   |||    |||||              |||                   |   ||   |                  |||||               |||||||||             ||    |||           |||||||||       
                      ||||                     ||||                  |      |||           |     ||      ||||              |||                       ||                       ||||                  ||||               ||     ||              ||||         
                       |||                      |||                         |||                          |||              |||                       ||                       |||                    |||||                                     |||||       
                       ||                       ||                           |                            |                ||                       ||                        ||                      |||||                                     |||||     
                                                                                                                                                    |                                                  |||||||                                   |||||||  
                                                                                                                                                                                                          ||||                                      ||||  
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                          
*/

P1340 兽径管理

测评记录 被卡常了求助

2024/6/20 21:38
加载中...