1 条题解

  • 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;
    }
    
    • 1

    信息

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