1 solutions

  • 0
    @ 2025-12-10 16:40:44
    #include<bits/stdc++.h>
    using namespace std;
    #define int long long
    struct no {
    	int id;
    	int num;
    	vector <int > edge;
    
    } a[200005], b[200005];
    int vis[200005], ans = -10000000000000000;
    bool cmp(no x, no y) {
    	return x.num < y.num;
    }
    void bfs( no i) {
    	queue<no> q;
    	q.push(i);
    	int mj = i.num;
    	vis[i.id] = 1;
    	while (!q.empty()) {
    		no temp = q.front();
    		vis[temp.id] = 1;
    		q.pop();
    		for (int j = 0; j < temp.edge.size(); j++) {
    			ans = max(ans, b[temp.edge[j]].num - mj);
    			if (!vis[temp.edge[j]])
    				q.push(b[temp.edge[j]]);
    
    		}
    	}
    
    
    
    }
    
    
    
    signed main() {
    		//freopen("188E.in","r",stdin);
    	//	 	freopen("188E.out","w",stdout);
    	int n, m, tp, st, ed;
    	cin >> n >> m;
    	for (int i = 1; i <= n; i++) {
    		cin >> tp;
    		a[i].id = i;
    		a[i].num = tp;
    		b[i].id = i;
    		b[i].num = tp;
    	}
    	for (int i = 1; i <= m; i++) {
    		cin >> st >> ed;
    		a[st].edge.push_back(ed);
    		b[st].edge.push_back(ed);
    	}
    
    	sort(a + 1, a + n + 1, cmp);
    	for (int i = 1; i <= n; i++) {
    		if (vis[a[i].id])continue;
    		bfs(a[i]);
    
    	}
    	cout << ans;
    
    }
    
    • 1

    Information

    ID
    583
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    4
    Tags
    # Submissions
    77
    Accepted
    16
    Uploaded By