2 条题解

  • 1
    @ 2024-11-5 22:40:10

    因为第一道题的题面有1019个字,第509个字是“的 ”,“的”的笔画数能被二整除,所以我们考虑二分。

    很明显,二分道路分值最大值的最小值。check 函数就直接判断能不能组成这样一颗树就好了。

    # include <bits/stdc++.h>
    using namespace std;
    
    struct node
    {
    	int u, v, w;
    }a[100012];
    
    struct path
    {
    	int v, w;
    	path (int v, int w) : v (v), w (w) {}
    };
    
    # define count chtholly
    
    int n, m, vis[311], count, cnt;
    	
    vector <path> e[100012];
    queue <int> q;
    
    bool check (int x)
    {
    //	cout << "right" << endl;
    	for (int i = 1; i <= n; i ++) e[i].clear ();
    //	q.clean ();
    	cnt = 0;
    	for (int i = 1; i <= m; i ++)
    	{
    		if (a[i].w <= x)
    		{
    			e[a[i].u].push_back (path (a[i].v, a[i].w));
    			e[a[i].v].push_back (path (a[i].u, a[i].w));
    		}
    	}
    	memset (vis, 0, sizeof vis);
    	q.push (1);
    	while (!q.empty ())
    	{
    		int u = q.front ();
    //		cout << u << endl;
    		q.pop ();
    		cnt ++;
    		if (vis[u]) continue;
    		vis[u] = 1;
    		for (int i = 0; i < e[u].size (); i ++)
    		{
    			int v = e[u][i].v;
    			q.push (v);
    		}
    	}
    	for (int i = 1; i <= n; i ++) if (!vis[i]) return 0;
    	return 1;
    }
    
    int main ()
    {
    	ios :: sync_with_stdio (0);
    	cin.tie (0), cout.tie (0);
    	cin >> n >> m;
    	for (int i = 1; i <= m; i ++)
    	{
    		cin >> a[i].u >> a[i].v >> a[i].w;
    	}
    	int l = 0, r = 1e5, mid, ans = 0;
    //	cout << "right" << endl;
    	while (l <= r)
    	{
    		mid = (l + r) / 2;
    //		cerr << "right" << endl;
    		if (check (mid)) r = mid - 1, ans = mid;
    		else l = mid + 1;
    	}
    	cout << n - 1 << ' ' << ans << endl;
    }
    
    • 0
      @ 2025-2-21 18:10:53
      #include <bits/stdc++.h>
      using namespace std;
      struct Node
      {
      	int x,y,t;
      } a[10010];
      int f[510];
      int find(int x)
      {
      	if (f[x]!=x) f[x]=find(f[x]);
      	return f[x];
      }
      bool cmp(Node a,Node b)
      {
      	return a.t<b.t;
      }
      int main()
      {
      	int n,m;
      	//freopen("city.in","r",stdin);
      	//freopen("city.out","w",stdout);
      	scanf("%d%d", &n, &m);
      	for(int i=1;i<=n;i++)
      		f[i]=i;
      	for(int i=1;i<=m;i++)
      		scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].t);
      
      	sort(a+1,a+m+1,cmp);
      	int cnt=n-1,ans=0;
      	for(int i=1;i<=m;i++)
      	{
      		int px=find(a[i].x);
      		int py=find(a[i].y);
      		if(px==py)continue;
      		cnt--;
      		ans++;
      		f[py]=px;
      		if (cnt==0)
      		{
      			cout<<ans<<" "<<a[i].t<<endl;
      			return 0;
      		}
      	}
      	return 0;
      }
      
      • 1

      信息

      ID
      6371
      时间
      1000ms
      内存
      125MiB
      难度
      3
      标签
      递交数
      2
      已通过
      2
      上传者