http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307
题意:
思路:
可以直接二分答案,然后dfs。
因为标签是并查集,所以我考虑了一下并查集,利用并查集不断向上回溯加负重,居然过了,只能说数据有点水。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<sstream> 6 #include<vector> 7 #include<stack> 8 #include<queue> 9 #include<cmath> 10 #include<map> 11 #include<set> 12 using namespace std; 13 typedef long long ll; 14 const int INF = 0x3f3f3f3f; 15 const int maxn=50000+5; 16 const int mod=1e9+7; 17 18 int n; 19 int p[maxn]; 20 int c[maxn],w[maxn],f[maxn]; 21 22 int main() 23 { 24 //freopen("in.txt","r",stdin); 25 while(~scanf("%d",&n)) 26 { 27 for(int i=0;i<n;i++) p[i]=i; 28 int ans=n; 29 for(int i=0;i<n;i++) 30 { 31 scanf("%d%d%d",&c[i],&w[i],&f[i]); 32 if(ans!=n) continue; 33 if(f[i]==-1) continue; 34 p[i]=f[i]; 35 int x=i; 36 while(true) 37 { 38 x=p[x]; 39 w[x]+=w[i]; 40 if(w[x]>c[x]) {ans=i;break;} 41 if(x==p[x]) break; 42 } 43 } 44 printf("%d\n",ans); 45 } 46 return 0; 47 }
时间: 2024-10-22 10:03:52